Difference Between Composition vs Aggregation
Composition and Aggregation are OOPS concepts used in any programming language. Composition explains that in a relationship no object can stay alone and each object is dependent on the parent. If the parent is destroyed, so is the child objects in the same class in Composition. Aggregation depicts the opposite that any object can stay alone without the parent. The objects are independent of the parent and hence even if the relationship is not present, objects cannot be destroyed. Composition is a part of Association but it depicts the relationship in a different manner when compared to Aggregation.
Head to Head Comparison between Composition vs Aggregation (Infographics)
Below are the top 7 differences between Composition vs Aggregation:
Key Differences between Composition vs Aggregation
Key differences between composition vs aggregation are given below:
- The relationship can be best explained with the help of examples. The human body has different parts like the heart, lungs, liver, and brain. When the human body is destroyed, the entire parts are also destroyed. The human body cannot survive by itself without the presence of any of these parts. This relationship is called Composition. Initially, it is said that the human body has a heart, the human body has lungs, and so on. This ‘has-a’ relationship is called Aggregation. The relationship of the heart to the human body is called aggregation. We also have a one-to-many relationship where we can say a human body has two eyes. Aggregation can be one-to-one, one-to-many, many-to-one relationships.
- When we define a class in a program, composite objects should remain in the same class and relationships cannot be made between two different classes if both classes are independent. The objects should remain in the same scope and in the same system. Aggregate objects can either be in a single class or a different class as it does not change the relationship of the objects. Objects should be in the same scope but not necessarily on the same system of objects i.e. class as explained here.
- Empty diamond is used to denote Composition. An arrow with a filled diamond represents the relation from parent to child that parent has with the child entity. Aggregation is represented by an empty diamond. An arrow with an empty diamond pointing to the child represents the relationship of parent and child where the child is independent.
- Both relationships of Composition and Aggregation can be concluded like this. When Aggregation provides ‘has a relationship, the composition provides ‘part of’ relationship. Hence Aggregation applies to the parts of a car where we can say the car has four wheels or car has an engine. But the working or operation cannot be specified with Aggregation. When we say wheels or engines are part of a car, the car cannot move without wheels or engines. This is Composition. They are not an entity by themselves to work on their own. Composition explains the owner of the object and depicts the parts of the parent as an entity.
- Both Composition and Aggregation comes under Association, a category for objects to depict the relationship between each other. Since the object relationship is dependent, Composition has a strong Association while Aggregation has a weak Association due to independence in relationships.
- The relationship is different for Composition and Aggregation. In Composition, Parents own child, and child objects have no existence if parents are not present. Whereas, in Aggregation, the relationship may or may not be present. If the relationship is present, then the parent Has-a a relationship with the child and there is no ownership.
In a programming language, relationships are important in a Class, be it Composition or Aggregation. Both relationships help in constituting the entire program for a successful output. We can share the objects with other classes and reassign the tasks to them in Aggregation and hence the requirements should be relative for each relationship.
Composition vs Aggregation Comparison Table
Comparison between Composition vs Aggregation are given below:
|A relationship is important in Composition, any objects or data are combined to form a relationship. Or in other words, the relationship is important for objects in Composition.||It is not necessary to maintain a relationship in Aggregation and hence any single objects or combined objects can be present in the Aggregation mechanism. There is no ownership of any objects.|
|The relationship between parent and child objects are dependent and hence child does not have its own lifetime. When the parent object is destroyed or removed, child object is also removed.||The relationship between parent and child objects are independent and hence child has its own lifetime. When the parent object is destroyed or removed, child object is not removed.|
|We cannot access the child objects directly. Also, child objects will be modified when parent objects are modified. The objects are interlinked due to dependency.||We can access child objects directly from the class. If needed, we can modify the child objects as well. In Aggregation, objects can exist either dependently or independently. Modification of parent objects does not affect child objects.|
|We can call composition a mixture where different objects are collected, formed a relationship, and made dependencies and exist with mutual understanding. Removal of one object removes the entire unit.||We can call Aggregation a collection where different objects are collected, formed a relationship but mutual understanding may not be present always. Removing one object does not affect any other object.|
|We cannot reassign or recreate the composite relationships in the class. If the objects are modified, they must be either made as aggregation or a different relationship must be created.||Since the objects are independent, we can reassign or recreate the objects and relationships in the class as it does not affect the entire system.|
|Objects are linked to each other and this linking is necessary to exist and to remain in the same system. This is called parent-child where the parent owns the child and the child does not have an existence.||Objects can be linked to each other without a parent and still can remain independent of any objects in the system.|
|Child do not have a lifetime of their own. Its lifetime is always dependent on parent. If parent lifetime is changed, child lifetime also changes.||Child has a lifetime of their own. Parents or any linked object’s lifetime does not affect child lifetime. Child lifetime can also change if needed.|
This is a guide to Composition vs Aggregation. Here we also discuss the Composition vs Aggregation key differences with infographics and a comparison table. You may also have a look at the following articles to learn more –