Introduction
In this article, we will try to understand 3 important concepts association, aggregation, and composition.
We will also try to understand what kind of scenarios we need them. These 3 concepts have confused lots of developers and in this article, I will attempt to present the concepts in a simplified manner with some real-world examples.
Extracting real-world relationships from the requirement
The whole point of OOP is that your code replicates the real-world object, thus making your code readable and maintainable. The time we say real world, the real world has relationships. Let's consider the simple requirements listed below.
- The manager is an employee of XYZ Limited Corporation.
- The manager uses a swipe card to enter XYZ premises.
- The manager has workers who work under him.
- The manager has the responsibility of ensuring that the project is successful.
- The manager's salary will be judged based on project success.
If you flesh out the above 5-point requirement we can easily visualize 4 relationships.
- Inheritance
- Aggregation
- Association
- Composition
Let's understand them one by one.
Requirement 1 (The IS-A relationship)
If you see 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 one the parent class "Employee" and the other 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. So we will not discuss inheritance in this article as it's pretty straight forward and I am sure you can get1000 of articles on the net which will help you in understanding the same.
Requirement 2 (The Using relationship: - Association)
Requirement 2 is interesting (the Manager uses a swipe card to enter XYZ premises). In this requirement, the manager object and swipe card object use each other but they have their own object lifetime. In other words, they can exist without each other. The most important point in this relationship is that there is no single owner.

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 the object of the "Manager" class and "SwipeCard" independently and they can have their object lifetime.
This relationship is called the "Association" relationship.
Requirement 3 (the using relationship with parent Aggregation)
The third requirement from our list (The manager has workers who work under him) denotes the same type of relationship as an association but with the difference that one of them is an owner. So as per the requirement, the "Manager" object will own the "Workers" object.
The child "Worker" objects can not belong to any other objects. For instance, the "Worker" object cannot belong to the "SwipeCard" object.
But....the "Worker" object can have its lifetime 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.




Harish SupekarPosted Aug 22, 2017, 5:37 AM
Hello Shivprasad sir, your all articles,videos are absolutely fantastic. The way you are using to explain concepts and your voice quality made me your biggest follower. Thanks a lot for sharing your knowledge.
pradip makavanaPosted Apr 6, 2015, 9:25 AM
Helpful Article.
Ashan DilanthaPosted Aug 22, 2013, 11:13 PM
Very helpful.. tnx.
Daisy KrausePosted Feb 16, 2012, 11:09 PM
Its a very nice and useful article.
Nitin SinghPosted Feb 16, 2012, 10:49 PM
Thanks for this article, nice work.