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.

 

Similar Reads

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....

Concept of Composition

Composition is a type of Aggregation in which two entities are extremely reliant on one another....

Concept of Aggregation

...

Drawback of Composition

Aggregation is a concept in which an object of one class can own or access another independent object of another class....

Why we should use Aggregation over Composition?

...

Contact Us