Javatpoint Logo
Javatpoint Logo

Difference Between Aggregation and Composition in Java

An object is a real-time entry, and objects have relationships between them both in programming or real life. Objects are related to each other using more than one relationship, such as Aggregation, Composition, Association, etc.

Let's understand the difference between Aggregation and Composition but before that we understand all these three relationships in detail.

Composition

Composition is a "belong-to" type of relationship in which one object is logically related with other objects. It is also referred to as "has-a" relationship. We can understand has-a or belong-to by using the following example:

A classroom belongs to a school, or we can say a school has a classroom. So, calling both of them is only a matter of point of view.

Composition is a strong type of "has-a" relationship because the containing object is its owner. So, objects are tightly coupled, which means if we delete the parent object, the child object will also get deleted with it.

Let's take an example to understand how we can implement the logic for the Composition relationship in Java:

CompositionExample.java

Output:

Aggregation vs Composition in Java

Aggregation

Aggregation relationship is also a "has-a" relationship. The only difference between Aggregation and Composition is that in Aggregation, objects are not tightly coupled or don't involve owning. All the objects are independent of each other and can exist even if the parent object gets deleted. Let's understand this by using the following example:

A car comes with a wheel, and if we take off its wheels, the wheels will still exist. But a car without wheels won't be as useful as a car with its wheels, and that's how this relationship existed in the first place for assembling the parts to a bigger construct, which is capable of more things than its parts.

Let's take an example to understand how we can implement the logic for the Aggregation relationship in Java:

Address.java

AggregationExample.java

Output:

Aggregation vs Composition in Java

Association

Association is not a has-a relationship and the weakest relationship among all. In an Association relationship, none of the objects are parts or members of another. A simple example of Association is a mother and her child.

Let's take an example to understand how we can implement the logic for Association relationships in Java:

AssociationExample.java

Output:

Aggregation vs Composition in Java

Difference Between Aggregation and Composition

Let's understand the differences between Aggregation and Composition:

S.No. Aggregation Composition
1. Association between two objects that defines the "has-a" relationship is referred to as Aggregation. A specific type of Aggregation that implies ownership is referred to as Composition.
2. In Aggregation, objects can remain in the scope of a system without each other. In a composition relationship, objects cannot remain in the scope of a system without each other.
3. In Aggregation, linked objects are independent of each other. In Composition, objects are tightly coupled or dependent on each other.
4. In Aggregation, if we delete the parent object, the child object will still exist in the system. For example, if we remove wheels from a car, the car can function adequately with another wheel. In Composition, if we delete a parent object, the child object also gets deleted. For example, if we delete a folder, the files will also get deleted which contain in the folder.
5. We represent it by using the filled diamond. We represent it by using the empty diamond.
6. In Aggregation, child objects don't have a lifetime. In Composition, child objects have a lifetime.
7. In Aggregation, if we delete an assembly, it will never affect its parts. In the case of owning a class, it affects the containing class object.






Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA