Aggregation vs. Composition

The key difference between aggregation and composition is that in aggregation there is a parent child relationship in which child can exist independently whereas composition is a parent child relationship in which child cannot exist independently without a parent.

Advertisement - Continue Reading Below
Aggregation vs. Composition

Association is a very important concept in object oriented programming. In object oriented programming the data is hidden from non-member functions of a class. Only the member function of a class can use data. Any non-member function can modify the data that is inside the function class. Object and classes are the main concepts of object oriented programming. Data encapsulation that is also known as abstraction and inheritance is also achieved in object oriented programming. Aggregation and composition are types of association and are a very important concept in object oriented programming. In aggregation, there is a parent child relationship in which a child can exist independently whereas composition is a parent child relationship in which a child cannot exist independently without a parent. Composition has a common paradigm in object oriented programming that is very important in software development. The restrictive form of association is a composition in which there is a parent child relationship in which the child cannot exist independently without a parent. In UML it is represented by a tiny solid diamond.

Advertisement - Continue Reading Below

In composition, the constituent part can be linked to most one in assembly. One class become container and other class become content that is in that container. That have inheritance part very easy, and method of one class can be used without even writing the whole code again. There is a “part of” relationship in composition. Aggregation is a relationship with an assembly class to constituent class. In UML that is unified modelling language that shows aggregation relationship with a line segment. Aggregation show relationship such as one to one, one to many, many to many. If we single class has multiple class, there is one to many relationships whereas many class has multiple objects is the many to one relationship. “Has a “is a relationship that is described in aggregation.

Contents: Difference between Aggregation and Composition

Comparison Chart

BasisAggregation Composition
MeaningIn aggregation, there is a parent child relationship in which the child can exist independently

The composition is a parent child relationship in which a child cannot exist independently without a parent.

Advertisement - Continue Reading Below

 

AssociationThere is a weak association in aggregation.There is a strong association in composition.
UMLIn UML aggregation can be defined by using the line segmentIn UML composition can be defined by using diamond.
FunctionDeletion of assembly has no effect in aggregation.Deletion of the composition can effect in the composition

 What is Aggregation?

Aggregation is a relationship with an assembly class to constituent class. In UML that is unified modelling language that shows aggregation relationship with a line segment. Aggregation show relationship such as one to one, one to many, many to many. If we single class has multiple class, there is one to many relationships whereas many class has multiple objects is the many to one relationship. “Has a “is a relationship that is described in aggregation.

What is Composition?

The restrictive form of association is a composition in which there is a parent child relationship in which the child cannot exist independently without a parent. In UML it is represented by a tiny solid diamond. In composition, the constituent part can be linked to most one in assembly. One class become container and other class become content that is in that container. That have inheritance part very easy, and method of one class can be use without even writing the whole code again. There is a “part of” relationship in composition.

Key Differences between Aggregation and Composition

  1. In aggregation there is a parent child relationship in which child can exist independently whereas Composition is a parent child relationship in which a child cannot exist independently without a parent.
  2. There is a weak association in aggregation whereas there is a strong association in composition whereas there is a strong association in composition.
  3. In UML aggregation can be defined by using line segment whereas In UML composition can be defined by using diamond.
  4. Deletion of assembly has no effect in aggregation whereas deletion of the composition can effect in the composition

Conclusion

In this article above we see the clear difference between aggregation and composition with examples.

Advertisement - Continue Reading Below

Explanatory Video

Leave a Comment