Composition refers to a fundamental concept that is included in the area of Object-Oriented Programming. With this concept, one would be able to describe a class that references one or more other classes objects as an instance variable. Moreover, with this concept, access to the members of one class inside another class takes place by using the class name or by creating the object.
Explanation of Composition
Composition enables creating complex types by facilitating a combination of objects belonging to different classes. Moreover, it means that a class Composite can have an object that belongs to another class Component. Most noteworthy, this type of relationship is called by experts as Has-A Relation.
For a better understanding of python, which is one of the computer languages, one can consider the representation of classes as boxes with the class name Composite and Component representing Has-A relation that exists between both of them.
So, class A :
# variables of class A
# methods of class A
Moreover, class B :
# by using “obj” one can access member’s of class A.
obj = A()
# variables of class B
# methods of class B
Browse more Topics Under Functions
- Importing Modules
- Invoking Built-in Functions
- Functions from Math Module
- Using Random and Randint Functions
# composite class constructor def __init__(self): print('Component class object created...') # composite class instance method Furthermore, def m1(self): print('Component class m1() method executed...') class Composite: # composite class constructor def __init__(self): # creating object belonging to component class self.obj1 = Component() Moreover, print('Composite class object also created...') # composite class instance method Also, def m2(self): print('Composite class m2() method executed...') # calling m1() method of component class self.obj1.m1() # creating object of composite class obj2 = Composite() # calling m2() method of composite class obj2.m2()
Component class object created... Furthermore, Composite class object also created... Moreover, Composite class m2() method executed... Component class m1() method executed...
- In the above example, the creation of two classes Composite and Component takes place to show the Has-A Relation present among them.
- In the Component class, there is one constructor as well as an instance method m1().
- Similarly, in the composite class, there is one constructor in which the creation of an object of the component Class takes place. Whenever there is the creation of an object of Composite Class, the automatic creation of the object of the Component class also takes place.
- Now, in the m2() method of Composite class, one may call m1() method of Component Class using instance variable obj1 in which the storing of a reference of Component Class takes place.
- Moreover, whenever the m2() method of Composite Class is called, m1() method of Component Class will be automatically called.
What is Inheritance
Inheritance refers to the concept of Object-Oriented Programming. Moreover, inheritance in python, which is one of the programming languages, is a mechanism that lets one inherit all the properties belonging to another class. Most noteworthy, the class from which the utilization of properties and functionalities takes place is the parent class or the base class.
The child class is the one that uses the properties from another class. Furthermore, the child class is also called the derived class. Moreover, experts also call inheritance an Is-A Relation.
For a better understanding, the representation of classes can take place as boxes. Furthermore, the representation of the inheritance relationship is by an arrow pointing from derived Class to base Class. Also, the extends keyword denotes that the inheritance or derivation of the child Class takes place from Parent Class.
# Parent class
Moreover, class Parent :
# Variables of Parent class
# Child class inheriting Parent class
Also, class Child(Parent) :
# constructor of child class
# variables of child class
# methods of child class
Inheritance in Python Examples
# parent class Furthermore, class Parent: # parent class method Also, def m1(self): print('Parent Class Method called...') # child class inheriting parent class Furthermore, class Child(Parent): # child class constructor def __init__(self): print('Child Class object created...') # child class method Moreover, def m2(self): print('Child Class Method called...') # creating object of child class obj = Child() # calling parent class m1() method obj.m1() # calling child class m2() method obj.m2()
Child Class object created... Parent Class Method called... Child Class Method called...
How to Choose Between Composition and Inheritance in Python
One important point to understand is that derived classes inherit the interface and implementation of their base classes. Furthermore, composition lets one reuse another class’s implementation.
Suppose the implementation of two solutions takes place to the same problem. Furthermore, in such a case, the first solution used multiple inheritances while the composition was used by the second one.
Python’s duck typing makes possible the reusing of objects with existing parts of a program by implementing the desired interface. Also, in Python, it is not a requirement to derive from a base class for the purpose of reusing the classes.
At this point, confusion may arise regarding whether to use inheritance or composition in python. Furthermore, the code reusing is enabled by both. Moreover, similar problems can be tackled by inheritance and composition in Python programs.
The general advice is to use the relationship that leads to the creation of fewer dependencies between two classes. Most noteworthy, such a relation is composition. However, the inheritance will make more sense at certain times.
Choosing between the two can be quite confusing for many people since both the concepts are pointing to code reusability. Furthermore, an important point to remember here is that the use of inheritance should take place where a class wants to derive the nature of the parent class and then go for the modification of its functionality of it. Moreover, the inheritance will extend the functionality with extra features, thereby allowing the overriding of methods.
When it comes to composition, we can only use that where the class is unable to extend or modify the functionality of it. Moreover, extra features will not be provided by it.
Compensation is recommended for one who has a need to use the class as it is without any modification. In contrast, inheritance is recommended when one needs to change the behaviour of the method in another class.
FAQs on Composition
Question 1: Differentiate between composition and inheritance?
Answer 1: Inheritance and composition, simply speaking, are two programming techniques used by developers to establish relationships between objects and classes. Furthermore, inheritance facilitates the derivation of one class from another class. In contrast, composition defines a class as the sum of all the parts it has.
This clearly shows the difference between composition and inheritance. Furthermore, contrary to what some may believe, inheritance is not one of the types of composition.
Question 2: What is meant by composition coding?
Answer 2: Composition, in the area of object-oriented programming, is one of the fundamental concepts. Furthermore, a class that references one or multiple objects belonging to other classes in instance variables is described by composition. Most noteworthy, composition lets one model a has-a association between objects.