Aggregation and Composition

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.

understand

I can see:

  1. A student
  2. A Library
  3. 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.

  1. A student can have a one-to-one relationship with the library.

  2. A library can have one-to-many relationships with books.

So, now there are the following 2 questions:

  1. Can a student object exist without a library?

    Answer: Yes, a student may or may not be associated with a library. This is Aggregation.

  2. 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:

  1. Both Aggregation and Composition are subsets of Association.

  2. Aggregation is a type of Association with has-a relationship.

    For example A library has a student.

  3. 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.


Similar Articles