What Is the Difference Between UML Aggregation and Composition?

An aggregation and a composition are both a type of association in UML diagrams. They are more specific versions of an association. An association connects two classes that need to communicate somehow. Both association and composition are kind of "has-a" relationships. Despite these similarities, there is a difference between aggregations and compositions.

UML Aggregation vs. Composition - What Have They Common?

An aggregation and a composition are UML associations. They are depicted as a line with a diamond on one end (although, different kinds of diamonds). The diamond points to the parent (container of aggregated objects), the unmarked line end should point to the children (aggregated objects).

UML Aggregation vs. UML Composition - Differences

An aggregation is a type of association that is depicted as a line with a hollow diamond on an end of the line. Aggregated objects can exist without their parent.

A composition is a type of association drawn as a line with a filled diamond on one side. Composed objects cannot exist with their parent.

Composition vs. Aggregation - UML
Composition vs. Aggregation - UML

The difference between them is that parts associated with a parent class using aggregations can be associated also to other classes and can exist with the parent class. On the contrary, the objects, that are associated using a composition, cannot exist without the class they are associated with.

UML Aggregation Example

We have three classes - a library department, a bookcase and a book. A library department is an aggregation of bookcases and a bookcase is an aggregation of books. A book can exist without a bookcase and also a bookcase can exist without the library department.

UML Aggregation Example
UML Aggregation Example

UML Composition Example

We have three classes representing real-word objects: a book, a chapter, and a page.

A book is a composition of multiple chapters. A chapter is a composition of multiple pages.

The pages cannot exist without their chapter and the chapters cannot exist without the book.

UML Composition Example
UML Composition Example

New Comment

Comment