Association, Aggregation and Composition

Association, aggregation and composition are three kind of relationships which classes can have in object oriented programming. Let’s understand the difference between them.

Table of Contents

1. Association
2. Aggregation
3. Composition
4. Summary

1. Association in Java

We call association those relationships whose objects have an independent lifecycle and where there is no ownership between the objects.

Let’s take an example of a teacher and student. Multiple students can associate with a single teacher, and a single student can associate with multiple teachers, but both have their own lifecycles (both can be create and delete independently); so when a teacher leaves the school, we don’t need to delete any students, and when a student leaves the school, we don’t need to delete any teachers.

association

2. Aggregation in Java

We call aggregation those relationships whose objects have an independent lifecycle, but there is ownership, and child objects cannot belong to another parent object.

Let’s take an example of a cell phone and a cell phone battery. A single battery can belong to a phone, but if the phone stops working, and we delete it from our database, the phone battery will not be deleted because it may still be functional. So in aggregation, while there is ownership, objects have their own lifecycle.

aggregation

3. Composition in Java

We use the term composition to refer to relationships whose objects don’t have an independent lifecycle, and if the parent object is deleted, all child objects will also be deleted.

Let’s take an example of the relationship between questions and answers. Single questions can have multiple answers, and answers cannot belong to multiple questions. If we delete questions, answers will automatically be deleted.

composition

4. Summary

Sometimes, it can be a complicated process to decide if we should use association, aggregation, or composition. This difficulty is caused in part because aggregation and composition are subsets of association, meaning they are specific cases of association.

Association, Aggregation and Composition Relationship
Association, Aggregation and Composition Relationship

Drop me your questions in comments section.

Happy Learning !!

Was this post helpful?

Join 7000+ Fellow Programmers

Subscribe to get new post notifications, industry updates, best practices, and much more. Directly into your inbox, for free.

9 thoughts on “Association, Aggregation and Composition”

  1. I have one question – i.e. can we say that tight coupling exist for Composition? because if we delete the parent object then its collabrated objects gets deleted also so can we say that it is tight coupling?

    One more – > Car has seats . Is this composition or Aggregation?

    Reply
    • One more – > Car has seats . Is this composition or Aggregation?
      To ans this question -> this is composition because without car seats can’t be exist. In simple term car contains seats so no car no seats. cheers. 🙂

      Reply
      • But if the Car is spoiled we can delete the car and shift the seats to a new car . So it is Aggregation . I think Engine and Car is more of Composition

        Reply
        • Actually No. It is a matter of implementation in code, rather than concept

          If Engine is created by third party code and passed into Car, then both lifecycles are independent, and it is a aggregation ( whole part relationship )

          If Engine is created by Car class directly, then Engine’s lifecycle is dependent on Car, and it is a composition (full ownership)

          Reply

Leave a Comment

HowToDoInJava

A blog about Java and its related technologies, the best practices, algorithms, interview questions, scripting languages, and Python.