DEV Community

Cover image for Association, Composition and Aggregation in Java
Satya Prakash Nandy
Satya Prakash Nandy

Posted on

Association, Composition and Aggregation in Java

In Object Oriented Programming, there can be broadly two types of relationships "IS-A" and "HAS-A" relationships.
We can achieve an "IS-A" relationship using Inheritance.
Whereas we can achieve a "HAS-A" relationship using Association.

Composition and Aggregation are two forms of Association.

Association.

Association represents a relationship between two separate classes where objects of one class are related to objects of another class. It is a loosely coupled relationship, meaning that the associated objects can exist independently.

The association can be one-to-one, one-to-many, many-to-one or many-to-many.

Example

Let's take an example of the Car and Engine class. In this example, Both car and engine classes can exist independently. The lifecycle of the Engine class object does not depend on the Car class object.

class Car {
    private Engine engine;

    public Car(Engine engine) {
        this.engine = engine;
    }
}

class Engine {
    // Engine class implementation
}
Enter fullscreen mode Exit fullscreen mode

Composition.

Composition represents a strong form of association where on class is composed of one or more object of another class. The composed object cannot exist without the existence of parent class.

When the parent object is destroyed all the composed objects are also destroyed.

Example.

In this example, the life cycle of the Engine object is completely dependent on the Car object. Once the Car object is destroyed, the associated Engine object will also be destroyed.
In this example, instead of passing an Engine object through the constructor, we are creating an Engine object inside the Car constructor. Hence, Engine and Car objects are tightly coupled with each other.

class Car {
    private Engine engine;

    public Car() {
        this.engine = new Engine();
    }
}

class Engine {
    // Engine class implementation
}
Enter fullscreen mode Exit fullscreen mode

Aggregation.

Aggregation is a specialized form of association where one class has a reference to another class, but the object can exist independently. It represents a “has-a” relationship, where the associated object can be shared among multiple objects.

Example

In this example, Department class contains a list of employee objects.
Each employee object can exist independently and their life cycle does not depend on the Employee object. Also multiple departments can share the same Employee object.

class Department {
    private List<Employee> employees;

    public Department() {
        employees = new ArrayList<>();
    }

    public void addEmployee(Employee employee) {
        employees.add(employee);
    }
}

class Employee {
    // Employee class implementation
}
Enter fullscreen mode Exit fullscreen mode

Conclusion

Aggregation and association are two forms of composition, where aggregation defines a 'HAS-A' relationship with a weak association, while composition represents a 'BELONGS-TO' relationship with a strong association.

Top comments (0)