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.
Python OOPS – Aggregation and Composition
In this article, we will compare and highlight the features of aggregation and Composition in Python OOPS.
Contact Us