Composition is an object-oriented design concept that models a has-a relationship. In composition, a class known as composite contains an object of another class known as component. In other words, a composite class has a component of another class1.1. Composition allows a class to be projected as a container of different classes.Composition is a concept in Python that allows for building complex objects out of simpler objects, by aggregating one or more objects of another class as attributes. The objects that are aggregated are generally considered to be parts of the whole object, and the containing object is often viewed as a container for the smaller objects.In composition, objects are combined in a way that allows for greater flexibility and modifiability than what inheritance can offer. With composition, it is possible to create new objects by combining existing objects, by using a container object to host other objects. By contrast, with inheritance, new objects extend the behavior of their parent classes, and are limited by that inheritance hierarchy.Reference:Official Python documentation onComposition:https://docs.python.org/3/tutorial/classes.html#compositionGeeksforGeeks article on Composition vs Inheritance:https://www.geeksforgeeks.org/compositionvs-inheritance-python/Real Python article on Composition and Inheritance: https://realpython.com/inheritancecomposition-python/
Composition is an object-oriented design concept that models a has-a relationship. In composition, a class known as composite contains an object of another class known as component. In other words, a composite class has a component of another class1.
1. Composition allows a class to be projected as a container of different classes.
Composition is a concept in Python that allows for building complex objects out of simpler objects, by aggregating one or more objects of another class as attributes. The objects that are aggregated are generally considered to be parts of the whole object, and the containing object is often viewed as a container for the smaller objects.
In composition, objects are combined in a way that allows for greater flexibility and modifiability than what inheritance can offer. With composition, it is possible to create new objects by combining existing objects, by using a container object to host other objects. By contrast, with inheritance, new objects extend the behavior of their parent classes, and are limited by that inheritance hierarchy.
Reference:
Official Python documentation on
Composition:https://docs.python.org/3/tutorial/classes.html#compositionGeeksforGeeks article on Composition vs Inheritance:
https://www.geeksforgeeks.org/compositionvs-inheritance-python/
Real Python article on Composition and Inheritance:
https://realpython.com/inheritancecomposition-python/