Aggregation in Java

Aggregation in Java is a relationship between two classes where one class is composed of one or more instances of another class. It represents a "has-a" relationship, where an object of one class "has" another object as a part of its state. Aggregation allows the creation of complex objects by combining simpler objects.

When to Use Aggregation

Aggregation is typically used in the following scenarios:

  1. Part-Whole Relationship: When one class represents a part or component of another class, aggregation is used to establish the relationship between the two. The aggregated class is considered a part of the aggregating class.

  2. Code Reusability: Aggregation promotes code reusability by allowing classes to reuse existing classes as part of their implementation. Rather than duplicating code or functionality, aggregation enables the composition of objects from other classes.

  3. Flexibility and Modularity: Aggregation enhances flexibility and modularity by providing a way to build complex objects from simpler components. It allows for the creation of higher-level objects that encapsulate the behavior and properties of the aggregated objects.

  4. Object Composition: Aggregation facilitates object composition, where objects are composed of other objects. It enables the creation of composite objects that consist of smaller, more specialized objects.

Example of Aggregation:

Let's consider an example of a University and Department relationship. A University consists of multiple departments, and each department is an independent entity. The University and Department classes can have an aggregation relationship.

class University {
    private String name;
    private List<Department> departments;

    public University(String name) {
        this.name = name;
        this.departments = new ArrayList<>();
    }

    public void addDepartment(Department department) {
        departments.add(department);
    }

    // Other methods and attributes
}

class Department {
    private String name;
    // Department-specific attributes and methods

    public Department(String name) {
        this.name = name;
    }

    // Other methods and attributes
}

In this example, the University class has an aggregation relationship with the Department class. The University class has a list of departments as one of its attributes, representing the "has-a" relationship. Multiple Department objects can be associated with a University object. The University class can add departments to its list through the addDepartment() method.

By using aggregation, the University and Department classes can maintain their independent existence while still establishing a relationship. It allows the University class to manage and interact with multiple departments as part of its functionality.


Overall, aggregation in Java enables the composition of objects, promoting code reusability, flexibility, and modularity. It is used to represent part-whole relationships and build complex objects from simpler components.