Difference between composition and aggregation


Both Composition and Aggregation are types of association which are used to represent the relationship between two classes. But they are absolutely different from each other. The basic difference between the two is that composition is a strong association, while aggregation is a weak association.

Read this article to learn more about composition and aggregation and how they are different from each other.

What is Composition?

Composition is a method of wrapping the simple objects or data types into a single unit. It is a type of association used to represent the relationship between two classes.

Composition is considered as a strong association type. This is because, in composition, the parent owns the child entity, so the child entity cannot exist without its parent entity. Thus, in composition, the child entity does not have its own life cycle. We cannot directly or independently access a child entity. In the UML diagram, composition is denoted by a filled diamond.

What is Aggregation?

Aggregation is another type of association that is used to represent the relationship between two classes. Aggregation is different from an ordinary composition because it gives information about a collection, and not about a mixture.

Aggregation does not imply any ownership on the child entity. In aggregation, the parent and the child entities maintain 'Has-A' relationship but both can also exist independently. We can use the parent and child entities independently. Any modification in the parent entity will not impact the child entity or vice versa.

In the UML diagram, aggregation is denoted by an empty diamond, which shows their obvious difference in terms of strength of the relationship.

Now, let us discuss the important differences between composition and aggregation in detail.

Difference between Composition and Aggregation

The following table gives the important differences between composition and aggregation −

Key

Composition

Aggregation

Basic

Composition is a way to wrap simple objects or data types into a single unit.

Aggregation differs from ordinary composition in that it does not imply ownership.

Relationship

In composition, parent entity owns child entity.

In Aggregation, parent Has-A relationship with child entity.

Tells about

Composition tells about a mixture.

Aggregation tells about a collection.

UML Notation

It is denoted by a filled diamond.

It is denoted by an empty diamond.

Life cycle

Child doesn't have their own life time.

Child can have their own life time.

Association

It is a strong association.

It is a weak association.

Conclusion

Both composition and aggregation are the types of association that are used to represent the relationships between two classes. The most significant difference that you should note here is that Composition is denoted by a filled diamond, whereas Aggregation is denoted by an empty diamond.

Updated on: 20-Feb-2023

34K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements