Aggregation in Java
A college department has teachers, but the teachers aren't parts of the department in the way a heart is part of a body. Close the department, and the teachers still exist; they can move to another department, or teach in two at once. That loose "has-a" relationship, where the parts have their own life, is called aggregation. In Java it's just a field that holds a reference to another object.
A Department Has Employees
import java.util.ArrayList;
import java.util.List;
public class College {
static class Employee {
final String name;
Employee(String name) { this.name = name; }
}
static class Department {
final String title;
private final List<Employee> staff = new ArrayList<>();
Department(String title) { this.title = title; }
void add(Employee e) { staff.add(e); }
String report() {
StringBuilder sb = new StringBuilder(title + ":");
for (Employee e : staff) sb.append(" ").append(e.name);
return sb.toString();
}
}
public static void main(String[] args) {
Employee navika = new Employee("Navika");
Employee srihitha = new Employee("Srihitha");
Employee advitha = new Employee("Advitha");
Department cs = new Department("Computer Science");
cs.add(navika);
cs.add(srihitha);
Department research = new Department("Research");
research.add(srihitha);
research.add(advitha);
System.out.println(cs.report());
System.out.println(research.report());
}
}Computer Science: Navika Srihitha
Research: Srihitha AdvithaHow it works
Departmenthas a fieldstaff: a list of references toEmployeeobjects (Listgets its own lessons later, with collections).- The employees are created outside the department (lines 26 to 28) and handed in with
add. - Srihitha belongs to both departments: the same object, referenced twice.
- If
cswere thrown away, all three employees would still exist;researchstill refers to two of them.
Try it yourself: add cs = null; before the last two lines and print only research.report(). Do the employees still exist?
Has-A vs Is-A
| Inheritance (is-a) | Aggregation (has-a) | |
|---|---|---|
| Sentence test | "A Manager is an Employee" | "A Department has Employees" |
| Java | class Manager extends Employee | Department has a List<Employee> field |
| Link fixed at | Compile time, for all objects | Runtime, per object; can change |
| Parts shared? | Not applicable | Yes, one part can belong to several wholes |
Common Mistakes
Using inheritance for has-a. class Car extends Engine makes a car a kind of engine.
Exposing the internal list. Returning staff directly lets callers add or remove employees without going through the department. Return a copy or an unmodifiable view (see "Defensive Copying in Java").
Assuming the whole controls the parts' lifetime. In aggregation it doesn't; when it does, that's composition (next lesson).
Practice
- Easy: Model
PlaylisthasSongs, where the same song can be in several playlists. - Harder: Add a method
boolean has(String name)toDepartment, and a method that moves an employee from one department to another.
Key Takeaways
Was this tutorial helpful?