Aggregation vs Composition

Aggregation and Composition are type of Association which explains how two elements are associated to each other in a class model. Sometime it very confusing to choose between Aggregation & Composition.


In Object orientation, Association is an important relationship which explains how one element is related to another by type and instance(s) ( i.e multiplicity ). Almost most of the objects that you would model or work with will associate with something or other.

class Person { String firstName; String lastName; Integer age; }

In the above example, the attribute firstName and lastName are associated to the class Person.

It is very easy to understand about the Association and it is straight forward. But the problem arises for most of the programmers during the UML modeling is, when they come across another type of association i.e Composition. Though they are very similar in the action, but they are applied for different purposes. Both are certainly an easy to understand concepts, but understanding them requires few insight / visualization of few real world things and correlation with the concepts.

Merriam Webster definition for

Aggregation:the collecting of units or parts into a mass or whole. Composition:product of mixing or combining various elements or ingredients.

Does both sounds similar? No. There is a subtle difference. Lets look at those differences now.


Aggregation is the process of grouping homogenous objects.

Notice the bold words homogenous objects. Yes, aggregation deals with same type of objects. Same type of object means? Let us see this example.

In Microsoft Excel, Aggregation is process of applying a specific function on set of similar object( Numbers, Text etc.,)

Microsoft Excel Aggregation example

So, if you mix numbers along with text, the aggregation will not work. Same applies for Object Oriented Modelling too.

Another example is Book. A book is aggregation of pages, not anything else.

class Book { Page[] pages; }

class DuckPond { List ducks; }

Ducks in Pond

Courtesy by John Morgan

In the above example, the list will hold same Address object.

In Java Generics in the Collections are kind of Aggregation.

List<Driver> driversList = new ArrayList<Driver>();


Composition on the other hand is again grouping of object but, dis-similar objects. Than Association you will model objects with composition more frequently. Composition reflects the objects in real world.

class Car { Wheel[] wheels; BodyStyle body; HeadLight[] headlights; }

The code builds a Car object, where a Car is composed of heterogenous parts like Wheels, Headlights, Body metals etc., Here the appropriate method is Composition. Aggregation wont fit here.

Let us consider the book example. A book is aggregation of pages, but pages are not just made of text. They are made of tex and pictures.

class Page { List textblocks; List imageblocks; }

Example of Composition: Perfect South Indian Dish

Composition of different and appropriate object makes things better

Published on: