What is Aggregation?

Introduction

Aggregation is one of the most important concepts in Object-Oriented Programming (OOP) that represents a weak “has-a” relationship between two classes. It is a specialized form of Association, where one object contains a reference to another object, but both objects can exist independently of each other.

In Java, classes often work together to model real-world relationships. For example, a Department has Employees, a University has Students, and a Library has Books. Although these objects are related, they have independent lifecycles. If a department is deleted, the employees still exist. Similarly, if a library closes, the books still exist. These relationships are examples of aggregation.

Aggregation promotes code reusability, loose coupling, and modular design. Instead of creating objects inside another class and tightly binding them together, aggregation allows one object to reference another object that is created independently.

What is Aggregation?

Aggregation is a relationship where one class contains a reference to another class, but both objects have independent lifecycles.

It represents a weak ownership relationship.

Definition:

Aggregation is a specialized form of association that represents a weak “has-a” relationship in which the contained object can exist independently of the container object.

Examples include:

  • Department → Employees
  • Library → Books
  • Team → Players
  • University → Students
  • Company → Departments

Even if the parent object is destroyed, the child objects continue to exist.

Syntax

Aggregation is implemented by storing an object reference inside another class.


class Employee {
}
class Department {
    private Employee employee;
}

Here, Department has an Employee, but the employee exists independently.

Basic Example of Aggregation


class Employee {
    String name;
    Employee(String name) {
        this.name = name;
    }
}
class Department {
    String departmentName;
    Employee employee;
    Department(String departmentName, Employee employee) {
        this.departmentName = departmentName;
        this.employee = employee;
    }
    void display() {
        System.out.println(employee.name + " works in " + departmentName);
    }
}
public class Main {
    public static void main(String[] args) {
        Employee employee = new Employee("Rahul");
        Department department = new Department("IT", employee);
        department.display();
    }
}

Output

Rahul works in IT

The Employee object is created independently and then passed to the Department, demonstrating aggregation.

How Aggregation Works

The parent object stores only a reference to the child object.


Department
     │
     │ references
     ▼
Employee

The Department uses the Employee, but it does not control the employee’s lifecycle.

Features of Aggregation

Aggregation has the following characteristics:

  • Represents a has-a relationship.
  • It is a weak form of association.
  • Parent and child objects have independent lifecycles.
  • Child objects can belong to multiple parent objects if appropriate.
  • Promotes loose coupling.
  • Supports object reusability.
  • Makes applications easier to maintain.

Real-Life Example

Consider a University Management System.


class Student {
    String name;
    Student(String name) {
        this.name = name;
    }
}
class University {
    String universityName;
    Student student;
    University(String universityName, Student student) {
        this.universityName = universityName;
        this.student = student;
    }
    void display() {
        System.out.println(student.name + " studies at " + universityName);
    }
}
public class Main {
    public static void main(String[] args) {
        Student student = new Student("Amit");
        University university = new University("ABC University", student);
        university.display();
    }
}

Output

Amit studies at ABC University

If the University object is destroyed, the Student object can still exist.

Multiple Objects in Aggregation

Aggregation commonly involves collections.

Example:


import java.util.ArrayList;
import java.util.List;
class Employee {
    String name;
    Employee(String name) {
        this.name = name;
    }
}
class Company {
    private List employees = new ArrayList();
    void addEmployee(Employee employee) 
        employees.add(employee)
    }
    void displayEmployees() {
        for (Employee employee : employees) {
            System.out.println(employee.name);
        }
    }
}

A company references multiple employees, but each employee can exist independently.

Aggregation vs Association

Aggregation Association
Specialized form of association General relationship
Weak ownership May or may not represent ownership
Represents a “has-a” relationship Represents a “uses-a” or “has-a” relationship
Child exists independently Objects exist independently
More specific relationship Broader relationship

Aggregation vs Composition

Aggregation Composition
Weak ownership Strong ownership
Child exists independently Child depends on parent
Child can exist without parent Child cannot exist without parent
Parent references child Parent owns child completely
Example: Department–Employee Example: House–Room

Think of the relationships like this:


Association
      │
      ├── Aggregation (Weak)
      │
      └── Composition (Strong)

Advantages of Aggregation

Aggregation offers several benefits:

  • Promotes loose coupling.
  • Encourages object reusability.
  • Simplifies maintenance.
  • Improves modularity.
  • Models real-world relationships naturally.
  • Supports scalable application design.
  • Allows independent object management.
  • Reduces code duplication.

Best Practices

Follow these best practices when using aggregation:

📖
Best Practices:
  • Use aggregation when objects should have independent lifecycles.
  • Pass dependent objects through constructors or setters instead of creating them internally.
  • Avoid unnecessary tight coupling between classes.
  • Clearly define object responsibilities.
  • Use interfaces where appropriate to reduce dependencies.
  • Prefer aggregation over inheritance when there is no true “is-a” relationship.
  • Keep relationships simple and meaningful.

Common Mistakes

Beginners often make these mistakes:

  • Confusing aggregation with composition.
  • Assuming every “has-a” relationship is composition.
  • Creating child objects inside the parent when aggregation is intended.
  • Using inheritance instead of aggregation.
  • Making objects overly dependent on each other.
  • Ignoring object lifecycles.
  • Creating unnecessary bidirectional relationships.

Conclusion

Java Aggregation is a specialized form of association that models a weak “has-a” relationship between objects. It allows one object to reference another while keeping both objects independent, making it ideal for representing real-world relationships such as departments and employees, universities and students, or libraries and books.

Aggregation promotes loose coupling, code reusability, and modular application design, helping developers create flexible and maintainable software. Understanding aggregation is also essential for distinguishing it from composition, where the parent owns the child and controls its lifecycle. Mastering aggregation enables you to design better object-oriented systems and lays a strong foundation for advanced Java design principles and architecture.

Related Java Tutorials