My earlier article was about Association in OOP. Then I got comments and feedback requesting that I write and explain more about other features. So, here we are going to talk about Aggregation and Composition in OOAD.
Look at the picture below and see what we can understand from it.
I can see:
- A student
- A Library
- Books
What does this mean?
Can we say that all of these are objects?
The answer is yes. Now try to establish a relationship between them. in other words Association.
- A student can have a one-to-one relationship with the library.
- A library can have one-to-many relationships with books.
So, now there are the following 2 questions:
- Can a student object exist without a library?
Answer: Yes, a student may or may not be associated with a library. This is Aggregation.
- Can a library object exist without books?
Answer: No, a library is of no use without books. In short, if there are no books there is no library. This is Composition.
Let us summarize what we have learned:
- Both Aggregation and Composition are subsets of Association.
- Aggregation is a type of Association with has-a relationship.
For example A library has a student.
- Composition has a direct dependency on the other objects. That means the object's existence depends on the other primary object.
For example, a library cannot exist without books. So if there are no books there is no library.
I hope you liked this article. If there is anything more expected then please provide comments.

Bhuvanesh MohankumarPosted May 10, 2016, 2:29 AM
Good one..
Gowtham RajamanickamPosted May 20, 2015, 1:18 AM
thanks for sharing with us
Prashant ShindePosted Jan 31, 2015, 12:46 PM
NO sir....you are correct
Prashant ShindePosted Jan 29, 2015, 12:08 PM
Hello Sir, Thanks for you comment. If we need to implement it practically then we need to create student outside Library class. I feel there is nothing like logical aggregation. Either we can have Aggregation or Composition. It will depend on how do you want to implement and that is what OOAD :-) I hope you agree.....
Jasminder SinghPosted Jan 29, 2015, 10:05 AM
I have a question regarding the concept of Aggregation which i have been looking for so long.Logically it seems to be fine that "student may or may not be associated with library" Does that means that in practical implementation of the above example, we will have the Student instance created outside the Library class and will be passed on to the library ??? In other words, practical implementation will require us to make sure where we are instantiating the classes either inside the main class or outside it ??? If we create the instance inside the Library class, practically it would result in a composition (which was logically a aggregation) ??
Manish Kumar ChoudharyPosted Jan 29, 2015, 6:57 AM
Nice one..