Association, Aggregation, and Composition in Java
In Object-Oriented Programming (OOP), classes often work together — objects interact and depend on each other.
These relationships between classes are known as Association, Aggregation, and Composition. They describe “how objects are related” and “how strongly one object depends on another.”
1. Association in Java
Association represents a relationship between two separate classes that are connected through their objects.
It shows how one object uses or interacts with another — but both objects can exist independently.
Example – Association
class Teacher {
String name;
Teacher(String name) { this.name = name; }
}
class Student {
String name;
Student(String name) { this.name = name; }
void studyWith(Teacher t) {
System.out.println(name + " studies with " + t.name);
}
}
public class AssociationExample {
public static void main(String[] args) {
Teacher teacher = new Teacher("Mr. Sharma");
Student student = new Student("Aman");
student.studyWith(teacher);
}
}
Output:
Aman studies with Mr. Sharma
Explanation:
[Student] ↔ [Teacher]
(Both can exist independently)- They are linked only through an association (temporary relationship).
- Neither owns nor depends on the other completely.
2. Aggregation in Java (Has-A Relationship)
Aggregation is a special form of association where one class contains a reference to another class, but both can still exist independently. It represents a “Has-A” relationship with weak ownership.
For example:
A Department has Teachers, but Teachers can exist even if the Department is deleted.
Example – Aggregation
class Department {
String name;
Department(String name) { this.name = name; }
}
class Teacher {
String name;
Department department; // Aggregation
Teacher(String name, Department department) {
this.name = name;
this.department = department;
}
void showInfo() {
System.out.println(name + " belongs to " + department.name + " department.");
}
}
public class AggregationExample {
public static void main(String[] args) {
Department dept = new Department("Computer Science");
Teacher t1 = new Teacher("Neha", dept);
Teacher t2 = new Teacher("Ravi", dept);
t1.showInfo();
t2.showInfo();
}
}
Output:
Neha belongs to Computer Science department.
Ravi belongs to Computer Science department.
Key Points of Aggregation
[Department]
↓
[Teacher 1]
[Teacher 2]
(Department has Teachers — but Teachers can exist independently)- Represents a “Has-A” relationship.
- Weak dependency — contained objects can exist without the container.
- Example: Department → Teacher, Library → Books, Company → Employees.
3. Composition in Java (Strong Association)
Composition is a stronger form of aggregation. Here, the lifetime of the contained object depends on the container object.
If the container (main object) is destroyed, contained objects are also destroyed.
Example – Composition
class Engine {
void start() {
System.out.println("Engine starts...");
}
}
class Car {
private Engine engine; // Composition
Car() {
engine = new Engine(); // Engine created inside Car
}
void startCar() {
engine.start();
System.out.println("Car is moving...");
}
}
public class CompositionExample {
public static void main(String[] args) {
Car car = new Car();
car.startCar();
}
}
Output:
Engine starts...
Car is moving...
Explanation:
[Car] → [Engine]
(Engine exists only as part of Car — strong ownership)- The
Carobject owns theEngineobject. - When the
Caris destroyed, itsEnginealso ceases to exist. - This is strong ownership → Composition.
Difference Between Association, Aggregation, and Composition
| Feature | Association | Aggregation | Composition |
|---|---|---|---|
| Definition | General relationship between two classes | Weak “Has-A” relationship | Strong “Has-A” relationship |
| Dependency | Objects are independent | Contained object can exist without container | Contained object cannot exist without container |
| Ownership | No ownership | Partial ownership | Full ownership |
| Lifetime | Independent | Separate lifetimes | Shared lifetime (dependent) |
| Example | Student ↔ Teacher | Department → Teacher | Car → Engine |
Real-World Examples
| Real-World Object | Relationship Type |
|---|---|
| Student ↔ Library | Association |
| Company → Employees | Aggregation |
| Human → Heart | Composition |
| Computer → CPU | Composition |
Points to Remember
- Association: Relationship without ownership.
- Aggregation: Weak ownership (“Has-A” but independent).
- Composition: Strong ownership (object depends fully).
- These relationships help design flexible, modular, and maintainable code.
Association → Student ↔ Library (Independent)
Aggregation → Company → Employees (Weak ownership)
Composition → Human → Heart (Strong ownership)
