HowToDoInJava

  • Python
  • Java
  • Spring Boot
  • Dark Mode
Home / Java Object Oriented Programming / Association, Aggregation and Composition

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?

Let us know if you liked the post. That’s the only way we can improve.
TwitterFacebookLinkedInRedditPocket

About Lokesh Gupta

A family guy with fun loving nature. Love computers, programming and solving everyday problems. Find me on Facebook and Twitter.

Feedback, Discussion and Comments

  1. Mehdi Afifi

    February 17, 2020

    Hi, please write an article about inheritance vs composition vs aggregation and which one is better to use.

  2. sreenath

    December 29, 2019

    Not at all, clear man. just a theory, even Wikipedia would have some code examples. Without code examples, this post has no value.

    • Lokesh Gupta

      December 29, 2019

      Thanks for the feedback. I will update the post.

  3. Valerii Synenko

    June 20, 2019

    Good explanation, but if it were some examples in the form of the code, it would be great.

  4. Arpit Agrawal

    September 26, 2017

    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?

    • Rajeev

      November 8, 2017

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

      • Zee

        October 9, 2018

        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

        • UkProgrammer

          February 18, 2019

          yup right

        • Onur Mete Kaplan

          May 15, 2019

          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)

Comments are closed on this article!

Search Tutorials

Java OOP

  • OOP – Introduction
  • OOP – Access Modifiers
  • OOP – Constructors
  • OOP – Instance Initializers
  • OOP – Abstraction
  • OOP – Encapsulation
  • OOP – Inheritance
  • OOP – Polymorphism
  • OOP – Overloading vs Overriding
  • OOP – Interface vs Abstract Class
  • OOP – extends vs implements
  • OOP – instanceof operator
  • OOP – Multiple Inheritance
  • Association, Aggregation and Composition

Java Tutorial

  • Java Introduction
  • Java Keywords
  • Java Flow Control
  • Java OOP
  • Java Inner Class
  • Java String
  • Java Enum
  • Java Collections
  • Java ArrayList
  • Java HashMap
  • Java Array
  • Java Sort
  • Java Clone
  • Java Date Time
  • Java Concurrency
  • Java Generics
  • Java Serialization
  • Java Input Output
  • Java New I/O
  • Java Exceptions
  • Java Annotations
  • Java Reflection
  • Java Garbage collection
  • Java JDBC
  • Java Security
  • Java Regex
  • Java Servlets
  • Java XML
  • Java Puzzles
  • Java Examples
  • Java Libraries
  • Java Resources
  • Java 14
  • Java 12
  • Java 11
  • Java 10
  • Java 9
  • Java 8
  • Java 7

Meta Links

  • About Me
  • Contact Us
  • Privacy policy
  • Advertise
  • Guest and Sponsored Posts

Recommended Reading

  • 10 Life Lessons
  • Secure Hash Algorithms
  • How Web Servers work?
  • How Java I/O Works Internally?
  • Best Way to Learn Java
  • Java Best Practices Guide
  • Microservices Tutorial
  • REST API Tutorial
  • How to Start New Blog

Copyright © 2020 · HowToDoInjava.com · All Rights Reserved. | Sitemap

  • Sealed Classes and Interfaces