The main difference between aggregation and composition is that aggregation is an association among two objects that have the “has a” relationship while the composition is a special type of aggregation that describes ownership.
Object-Oriented Programming (OOP) is a major paradigm in software engineering. According to OOP, the objects communicate with each other. A class is a blueprint to create objects while an association is a connection between two or more objects. Aggregation and composition are two types of association that describes the relationships among objects or classes.
Key Areas Covered
1. What is Aggregation
– Definition, Functionality
2. What is Composition
– Definition, Functionality
3. What is the Difference Between Aggregation and Composition
– Comparison of Key Differences
Key Terms
Aggregation, Composition, OOP, UML
What is Aggregation
Aggregation is an association that describes “has a” relationship between objects. For example, a classroom and student are linked with “has a” relationship.
When considering the above classroom and student scenario, the student object does not depend on the classroom object. Moreover, destroying the classroom does not affect or destroy the student object. Therefore, the association between classroom and student is an aggregation. Furthermore, Unified Modelling Language (UML) is a visual modelling language that helps to develop software systems. In UML, the diamond symbol represents the aggregation.
What is Composition
The composition is a special type of aggregation that describes ownership. An example is as follows.
In the above diagram, the School object contains classroom objects. As the school object contains classroom objects, it is an aggregation, but it is a special type of aggregation. If we destroy the school object, the classroom object is also destroyed. Therefore, it is a composition, and it implies ownership. In UML, the highlighted diamond symbol represents the composition.
Difference Between Aggregation and Composition
Definition
Aggregation is an association between two objects which describes the “has a” relationship while the composition is the most specific type of aggregation that implies ownership. Thus, this is the main difference between aggregation and composition.
Functionality
In aggregation, destroying the owning object does not affect the containing object. However, in composition, destroying the owning object affects the containing object. Hence, this is another difference between aggregation and composition.
UML Representation
Also, the diamond symbol represents the aggregation in UML while the highlighted diamond symbol represents the composition in UML.
Conclusion
In OOP, the objects communicate with each other. Here, the relationship between objects is an association. Aggregation and composition are two types of associations between objects or classes. The difference between aggregation and composition is that aggregation is an association among two objects that have the “has a” relationship while the composition is a special type of aggregation that describes ownership.
Reference:
1. 6 5 Using Aggregation and Composition, People Career, 22 May 2015, Available here.
Leave a Reply