Python OOPS – Aggregation and Composition
Last Updated :
17 May, 2024
In this article, we will compare and highlight the features of aggregation and Composition in Python OOPS.
Concept of Inheritance
Inheritance is a mechanism that allows us to take all of the properties of another class and apply them to our own. The parent class is the one from which the attributes and functions are derived (also called as Base Class). Child Class refers to a class that uses the properties of another class (also known as a Derived class). An Is-A Relation is another name for inheritance.
Syntax:
# Parent class
class Parent :
# Constructor
# Variables of Parent class
# Methods
...
# Child class inheriting Parent class
class Child(Parent) :
# constructor of child class
# variables of child class
# methods of child class
...
Concept of Composition
Composition is a type of Aggregation in which two entities are extremely reliant on one another.
- It indicates a relationship component.
- Both entities are dependent on each other in composition.
- The composed object cannot exist without the other entity when there is a composition between two entities.
Python3
class Salary:
def __init__( self , pay, bonus):
self .pay = pay
self .bonus = bonus
def annual_salary( self ):
return ( self .pay * 12 ) + self .bonus
class EmployeeOne:
def __init__( self , name, age, pay, bonus):
self .name = name
self .age = age
self .obj_salary = Salary(pay, bonus)
def total_sal( self ):
return self .obj_salary.annual_salary()
emp = EmployeeOne( 'Geek' , 25 , 10000 , 1500 )
print (emp.total_sal())
|
Output:
121500
Now as we can see in the above code we have successfully called the method of a completely different class inside another class that does not inherit the class using the concept of Composition.
Concept of Aggregation
Aggregation is a concept in which an object of one class can own or access another independent object of another class.
- It represents Has-A’s relationship.
- It is a unidirectional association i.e. a one-way relationship. For example, a department can have students but vice versa is not possible and thus unidirectional in nature.
- In Aggregation, both the entries can survive individually which means ending one entity will not affect the other entity.
Python3
class Salary:
def __init__( self , pay, bonus):
self .pay = pay
self .bonus = bonus
def annual_salary( self ):
return ( self .pay * 12 ) + self .bonus
class EmployeeOne:
def __init__( self , name, age, sal):
self .name = name
self .age = age
self .agg_salary = sal
def total_sal( self ):
return self .agg_salary.annual_salary()
salary = Salary( 10000 , 1500 )
emp = EmployeeOne( 'Geek' , 25 , salary)
print (emp.total_sal())
|
Output:
121500
From the above code, we will get the same output as we got before using the Composition concept. But the difference is that here we are not creating an object of the Salary class inside the EmployeeOne class, rather than that we are creating an object of the Salary class outside and passing it as a parameter of EmployeeOne class which yields the same result.
Drawback of Composition
As we saw in the previous snippet, we are creating an object of the Salary class inside EmployeeOne class which has no relation to it. So from the outside, if we delete the object of EmployeeOne class i.e emp in this case, then the object of Salary class i.e obj_salary will also be deleted because it completely depends upon the EmployeeOne class and its objects. To solve this dependency problem, Aggregation came into the picture.
Why we should use Aggregation over Composition?
Let’s take an example of both Composition and Aggregation to see the difference between them, and understand both precisely.
- In the case of Composition, if we delete the object emp then the object of the Salary class which we initialized inside the EmployeeOne class will be deleted automatically because it is completely dependent upon the object of EmployeeOne class, so it might cause some error in the output code.
But in the case of Aggregation, we can see that we have created completely two different objects emp and salary, and passed the salary object as a parameter to the EmployeeOne class, so even if we delete the emp object, the object of the Salary class will remain the same and we can also use that elsewhere.
- In the case of Composition, the objects were interdependent on each other, but in Aggregation, they are Unidirectional means that as salary is a completely independent object we can only pass salary to the emp, not vice versa.
- Composition is defined by the PART-OF relationship which means that one object IS PART-OF ANOTHER OBJECT, but Aggregation is defined by the HAS-A relationship which means that one object HAS-A RELATION with another object.
Please Login to comment...