Click here to Skip to main content
14,691,039 members
Articles » Development Lifecycle » Design and Architecture » General
Posted 15 Feb 2012

Tagged as


184 bookmarked

Understanding Association, Aggregation, and Composition

Rate me:
Please Sign up or sign in to vote.
4.82/5 (126 votes)
15 Mar 2012CPOL
In this article we will try to understand 3 important concepts association, aggregation and composition.

Table of contents


In this article, we will try to understand three important concepts: association, aggregation, and composition.

We will also try to understand in what kind of scenarios we need them. These three concepts have really confused a lot of developers and in this article, my attempt would be to present the concepts in a simplified manner with some real world examples.

Extracting real world relationships from a requirement

The whole point of OOP is that your code replicates real world objects, thus making your code readable and maintainable. When we say real world, the real world has relationships. Let’s consider the simple requirement listed below:

  1. Manager is an employee of XYZ limited corporation.
  2. Manager uses a swipe card to enter XYZ premises.
  3. Manager has workers who work under him.
  4. Manager has the responsibility of ensuring that the project is successful.
  5. Manager's salary will be judged based on project success.

If you flesh out the above five point requirement, we can easily visualize four relationships:-

  • Inheritance
  • Aggregation
  • Association
  • Composition

Let’s understand them one by one.

Requirement 1: The IS A relationship

If you look at the first requirement (Manager is an employee of XYZ limited corporation), it’s a parent child relationship or inheritance relationship. The sentence above specifies that Manager is a type of employee, in other words we will have two classes: parent class Employee, and a child class Manager which will inherit from the Employee class.

Note: The scope of this article is only limited to aggregation, association, and composition. We will not discuss inheritance in this article as it is pretty straightforward and I am sure you can get 1000s of articles on the net which will help you in understanding it.

Requirement 2: The Using relationship: Association

Requirement 2 is an interesting requirement (Manager uses a swipe card to enter XYZ premises). In this requirement, the manager object and the swipe card object use each other but they have their own object life time. In other words, they can exist without each other. The most important point in this relationship is that there is no single owner.

Image 1

The above diagram shows how the SwipeCard class uses the Manager class and the Manager class uses the SwipeCard class. You can also see how we can create objects of the Manager class and SwipeCard class independently and they can have their own object life time.

This relationship is called the “Association” relationship.

Requirement 3: The Using relationship with Parent: Aggregation

The third requirement from our list (Manager has workers who work under him) denotes the same type of relationship like association but with a difference that one of them is an owner. So as per the requirement, the Manager object will own Worker objects.

The child Worker objects can not belong to any other object. For instance, a Worker object cannot belong to a SwipeCard object.

But… the Worker object can have its own life time which is completely disconnected from the Manager object. Looking from a different perspective, it means that if the Manager object is deleted, the Worker object does not die.

This relationship is termed as an “Aggregation” relationship.

Image 2

Requirements 4 and 5: The Death relationship: Composition

The last two requirements are actually logically one. If you read closely, the requirements are as follows:

  1. Manager has the responsibility of ensuring that the project is successful.
  2. Manager's salary will be judged based on project success.

Below is the conclusion from analyzing the above requirements:

  1. Manager and the project objects are dependent on each other.
  2. The lifetimes of both the objects are the same. In other words, the project will not be successful if the manager is not good, and the manager will not get good increments if the project has issues.

Below is how the class formation will look like. You can also see that when I go to create the project object, it needs the manager object.

Image 3

This relationship is termed as the composition relationship. In this relationship, both objects are heavily dependent on each other. In other words, if one goes for garbage collection the other also has to be garbage collected, or putting from a different perspective, the lifetime of the objects are the same. That’s why I have put in the heading “Death” relationship.

Putting things together

Below is a visual representation of how the relationships have emerged from the requirements.

Image 4

The source code

You can download the sample source code for this article.


To avoid confusion henceforth for these three terms, I have put forward a table below which will help us compare them from three angles: owner, lifetime, and child object.

OwnerNo ownerSingle ownerSingle owner
Life timeHave their own lifetimeHave their own lifetimeOwner's life time
Child objectChild objects all are independentChild objects belong to a single parentChild objects belong to a single parent

Video on Association, Aggregation, and Composition

I have also added a video in case you do not want to read this long article.

<object type="application/x-shockwave-flash" codebase=",0,0,0" width="640" height="360" data=""><param name="movie" value="" /><param name="quality" value="high" /><param name="wmode" value="transparent" />

Just a note: I have recorded around 500 videos, do have a look at my videos on .NET, OOP, SQL Server, WCF, Silver light , LINQ , VSTS, SharePoint, Design Patterns, UML and a lot more.

Thanks for reading my article.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Comments and Discussions

GeneralAws Tutorial Pin
Member 1383712331-May-18 4:36
MemberMember 1383712331-May-18 4:36 
QuestionThe Composition code example is incorrect Pin
Mallanagouda Patil15-Sep-16 1:22
professionalMallanagouda Patil15-Sep-16 1:22 
QuestionConfusion between Association and Dependency Pin
Manuel Malagon14-Sep-15 9:26
MemberManuel Malagon14-Sep-15 9:26 
AnswerRe: Confusion between Association and Dependency Pin
Anandhan Balasubramnainan5-Nov-15 1:18
MemberAnandhan Balasubramnainan5-Nov-15 1:18 
GeneralThanks Pin
Desai_Developer23-Aug-15 5:46
MemberDesai_Developer23-Aug-15 5:46 
QuestionAggregation - Child objects belong to a single parent Pin
Amit_Rastogi22-Jul-15 0:45
MemberAmit_Rastogi22-Jul-15 0:45 
QuestionComposition - Death Condition? Pin
sushil_838-Jul-15 22:49
Membersushil_838-Jul-15 22:49 
GeneralMy vote of 5 Pin
nishant1233-May-15 0:21
Membernishant1233-May-15 0:21 
QuestionI'll give it 5 star Pin
Member 115800544-Apr-15 1:02
MemberMember 115800544-Apr-15 1:02 
QuestionThanks Great Explanation... Pin
Member 1123528214-Nov-14 22:08
MemberMember 1123528214-Nov-14 22:08 
QuestionThanks Pin
Amar Panigrahy2-Nov-14 7:01
MemberAmar Panigrahy2-Nov-14 7:01 
GeneralMy vote of 5 Pin
Yemi Dicy29-Sep-14 23:31
MemberYemi Dicy29-Sep-14 23:31 
GeneralMy vote of 5 Pin
Programmer Andrew10-Sep-14 20:36
MemberProgrammer Andrew10-Sep-14 20:36 
GeneralMy vote of 3 Pin
yagnam ramesh babu26-Jul-14 6:36
Memberyagnam ramesh babu26-Jul-14 6:36 
GeneralMy vote of 5 Pin
Chris Dis30-Jun-14 22:28
professionalChris Dis30-Jun-14 22:28 
Generalthank you Pin
ctgarvey28-Jan-14 14:49
Memberctgarvey28-Jan-14 14:49 
GeneralGood Job Pin
Swarup Batabyal26-Oct-13 3:52
MemberSwarup Batabyal26-Oct-13 3:52 
GeneralMy vote of 2 Pin
novi.kumala8-Oct-13 19:37
Membernovi.kumala8-Oct-13 19:37 
QuestionAwesome + Superb Pin
Rajesh Bisht2-Oct-13 22:13
MemberRajesh Bisht2-Oct-13 22:13 
GeneralMy vote of 5 Pin
Shahzad.ilyas7-Aug-13 2:46
MemberShahzad.ilyas7-Aug-13 2:46 
QuestionMy vote of 5 Pin
İlker Kargi21-Jul-13 9:50
Memberİlker Kargi21-Jul-13 9:50 
GeneralMy vote of 5 Pin
Hamza chaudhary4-Jul-13 12:15
MemberHamza chaudhary4-Jul-13 12:15 
GeneralMy vote of 5 Pin
Shrilata Ellaboina9-May-13 1:12
MemberShrilata Ellaboina9-May-13 1:12 
GeneralMy vote of 5 Pin
Vani Kulkarni6-May-13 0:02
professionalVani Kulkarni6-May-13 0:02 
Questionaggregation Pin
nasirkhayam14-Mar-13 22:47
Membernasirkhayam14-Mar-13 22:47 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.