Aggregation in Java

Naveen AnamaneniUpdated · 4 min read

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

The department refers to employees that live on their own
College.java▶ Try it
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());
    }
}
Output
Computer Science: Navika Srihitha
Research: Srihitha Advitha

How it works

  1. Department has a field staff: a list of references to Employee objects (List gets its own lessons later, with collections).
  2. The employees are created outside the department (lines 26 to 28) and handed in with add.
  3. Srihitha belongs to both departments: the same object, referenced twice.
  4. If cs were thrown away, all three employees would still exist; research still 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"
Javaclass Manager extends EmployeeDepartment has a List<Employee> field
Link fixed atCompile time, for all objectsRuntime, per object; can change
Parts shared?Not applicableYes, 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

  1. Easy: Model Playlist has Songs, where the same song can be in several playlists.
  2. Harder: Add a method boolean has(String name) to Department, and a method that moves an employee from one department to another.

Quick check: Aggregation

4 questions
1What is aggregation?
2How is aggregation written in Java?
3Can one employee belong to two departments in this design?
4Which sentence describes aggregation?

Key Takeaways

Was this tutorial helpful?

Lesson 117 of 482