VOOZH about

URL: https://www.geeksforgeeks.org/java/association-composition-aggregation-java/

⇱ Association, Composition and Aggregation in Java - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Association, Composition and Aggregation in Java

Last Updated : 30 May, 2026

In object-oriented programming, relationships between classes play a crucial role in defining how objects interact with each other. Java, being an object-oriented language, provides mechanisms to model these relationships through association, aggregation, and composition.

  • These relationships help model interactions between objects in real-world applications.
  • They define the level of dependency and ownership between classes.
  • Understanding them improves software design, maintainability, and code reusability.

👁 Association, Aggregation, and Composition

Association

Association is a relationship between two independent classes where one object uses or interacts with another object. Both objects can exist independently of each other.

  • Represents a Uses-A relationship.
  • Objects are loosely coupled.
  • Both classes can exist independently.

Types of Association

  • Unidirectional Association: One class knows about another, but not vice versa. Example: A Student has a LibraryCard, but the LibraryCard doesn't know about the Student.
  • Bidirectional Association: Both classes know about and interact with each other. Example: A Teacher is assigned to a Classroom, and the Classroom knows its Teacher.

Example:


Output
Ridhi belongs to bank ICICI
Vijay belongs to bank ICICI

Explanation: In the above example, two separate classes Bank and Employee are associated through their Objects. Bank can have many employees, So, it is a one-to-many relationship. 

👁 Association in Java

Aggregation

Aggregation is a special form of Association that represents a Has-A relationship with weak ownership. The contained object can exist independently of the container object.

  • Represents a Has-A relationship.
  • It is a weak association.
  • Child objects can exist independently of the parent object.
👁 Aggregation in Java

Example:


Output
Total students in institute: 4

Explanation:

  • There is an Institute which has no. of departments like CSE, EE. Every department has no. of students.
  • So, we make an Institute class that has a reference to Object or no. of Objects (i.e. List of Objects) of the Department class.
  • That means Institute class is associated with Department class through its Object(s).
  • And Department class has also a reference to Object or Objects (i.e. List of Objects) of the Student class means it is associated with the Student class through its Object(s). 

It represents a Has-A relationship. In the above example: Student Has-A name. Student Has-A ID. Department Has-A Students as depicted from the below media. 

👁 Aggregation Example

Note: Code reuse is best achieved by aggregation.  

Composition

Composition is a strong form of Aggregation that represents a Part-Of relationship. In Composition, the lifecycle of the contained object depends completely on the container object.

  • Represents a Part-Of relationship.
  • Strong ownership exists between objects.
  • Child objects cannot exist without the parent object.
👁 Composition

Example:


Output
Total Rooms: 4
Room names: 
- Living Room
- Bedroom
- Kitchen
- Bathroom

Explanation: In the above example, if the house is destroyed, its room also get destroyed this represents a complete composition relationship, where the room is the part of the house and the life cycle of room completely depends on the house.

Difference Between Association, Aggregation and Composition

Feature

Association

Aggregation

Composition

Definition

General relationship between two classes

A special form of association with a "has-a" relationship

A stronger form of association with a "part-of" relationship

Dependency

Classes are related but can exist independently

Contained objects can exist independently of the container object

Contained objects cannot exist without the container object

Lifecycle

Independent lifecycles

Independent lifecycles

Dependent lifecycles

Ownership

No ownership implied

Shared ownership

Exclusive ownership

Strength

Weak

Moderate

Strong

Cardinality

One-to-one, one-to-many, many-to-one, many-to-many

One-to-one, one-to-many, many-to-one, many-to-many

One-to-one, one-to-many

Example

Teacher and Student

Library and Books

Car and Engine

Representation

Uses a direct reference

Uses a reference to the contained object(s)

Contains instances of the contained object(s)

Illustrative Example Code

java class
Teacher { private
Student student;
}

java class
Library { private
List<Book> books;
}

java class
Car { private
Engine engine;
}

Comment
Article Tags:
Article Tags: