Clean β’ Professional
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.β
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)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.
[Department]
β
[Teacher 1]
[Teacher 2]
(Department has Teachers β but Teachers can exist independently)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)Car object owns the Engine object.Car is destroyed, its Engine also ceases to exist.| 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 Object | Relationship Type |
|---|---|
| Student β Library | Association |
| Company β Employees | Aggregation |
| Human β Heart | Composition |
| Computer β CPU | Composition |
Association β Student β Library (Independent)
Aggregation β Company β Employees (Weak ownership)
Composition β Human β Heart (Strong ownership)Β