Java Aggregation
Java aggregation is an important concept in Object-Oriented Programming (OOP). It represents a HAS-A relationship between two classes, where one class contains a reference to another class. Aggregation allows objects to work together while remaining independent of each other.
Unlike inheritance, aggregation does not create an IS-A relationship. Instead, it models situations where one object uses another object to perform its tasks. Aggregation improves code reusability, flexibility, and maintainability.
What is Aggregation in Java?
Aggregation is a special form of association in which one class owns or contains another class as one of its members. The contained object can exist independently of the container object.
For example, a Department has multiple Teacher objects. Even if the Department object is removed, the Teacher objects can still exist.
Java Aggregation Example
class Address{
String city;
Address(String city) {
this.city = city;
}
}
class Student {
String name;
Address address;
Student(String name, Address address) {
this.name = name;
this.address = address;
}
void display() {
System.out.println(name + " lives in " + address.city);
}
}
public class Main{
public static void main(String[] args) {
Address addr = new Address("Delhi");
Student s = new Student("Rahul", addr);
s.display();
}
}
How Aggregation Works
In aggregation, one object keeps a reference to another object instead of extending it. The referenced object is passed through the constructor or a setter method. Since both objects are independent, destroying one object does not automatically destroy the other.
Characteristics of Aggregation
HAS-A Relationship
One class contains another class as a member.
Independent Objects
Both objects can exist separately from each other.
Code Reusability
Existing classes can be reused without modification.
Loose Coupling
Reduces dependency between classes.
Real-World Examples of Aggregation
- A Student has an Address.
- A Department has Teachers.
- A Library has Books.
- A Company has Employees.
- A Team has Players.
Advantages of Aggregation
- Promotes code reuse.
- Creates flexible and maintainable applications.
- Represents real-world relationships naturally.
- Supports loose coupling between classes.
- Allows independent object lifecycles.
Aggregation vs Inheritance
| Aggregation | Inheritance |
|---|---|
| Represents HAS-A relationship. | Represents IS-A relationship. |
| Uses object references. | Uses the extends keyword. |
| Objects are independent. | Child depends on parent class. |
| Provides loose coupling. | Creates tighter coupling. |
When Should You Use Aggregation?
Use aggregation when one object needs to use another object without owning its complete lifecycle. It is suitable when objects should remain independent but still work together. Aggregation is commonly used in enterprise applications, management systems, and object modeling because it creates modular and reusable software.
Summary
Java aggregation is a HAS-A relationship in which one class contains a reference to another class. Unlike inheritance, both objects can exist independently. Aggregation improves code reuse, reduces coupling, and models real-world relationships effectively. It is one of the fundamental OOP concepts used to build flexible and maintainable Java applications.