• Articles
  • Tutorials
  • Interview Questions

Composition and Aggregation in Java - Examples and Applications

Composition in Java

Composition stands as a foundational principle in the realm of object-oriented programming, delineating a “has-a” connection between classes. This concept involves the assembly of intricate entities through the fusion of simpler constituents. Within the context of Java, composition involves the instantiation of one class’s instances within another class, frequently culminating in a more robust and encapsulated interconnection between these two classes.

In the composition paradigm, a singular class incorporates an instance of another class as a constituent variable. The included object doesn’t possess an autonomous existence apart from the encompassing class. Should the enclosing entity be removed, the encompassed object is correspondingly eradicated. This intrinsic closeness guarantees meticulous control over the lifecycle of the enclosed object, intricately bound to the lifecycle of its container.

class Engine {
    // Engine implementation
}
class Car {
    private Engine engine;
    public Car() {
        engine = new Engine();
    }
    // Other methods and functionalities
}

In the example above, the Car class contains an instance of the Engine class. The Car class manages the creation and lifecycle of the Engine instance. If a Car object is deleted, its associated Engine object is also effectively discarded.

Aggregation in Java

Aggregation is another form of association between classes, indicating a “part-of” or “whole-part” relationship. In contrast to composition, aggregation implies that the associated objects can exist independently of each other. One class may hold references to instances of another class, but the lifecycles of these instances are not necessarily tied together.

In Java, aggregation is often used to model scenarios where one class represents a collection of objects while still maintaining independence.

class Department {
    private List<Employee> employees;
    public Department(){
        employees = new ArrayList<>();
    }
    public void addEmployee(Employee employee) {
        employees.add(employee);
    }
    // Other properties and methods
}

In this example, the Department class holds a list of Employee instances through aggregation. Each Employee instance can exist on its own, and a change in the Department does not necessarily affect the Employee instances outside that context.

Choosing Between Composition and Aggregation in Java

When deciding between composition and aggregation, consider the nature of the relationship between classes. Composition is appropriate when one class is an integral part of another class and should not exist without it. Aggregation is suitable when one class holds another class as a component, but the component can exist independently.

Both composition and aggregation play vital roles in designing efficient and maintainable Java code. They allow developers to represent real-world relationships accurately and create well-structured systems that reflect the complexity of the world they model.

Course Schedule

Name Date Details
Python Course 23 Nov 2024(Sat-Sun) Weekend Batch View Details
30 Nov 2024(Sat-Sun) Weekend Batch
07 Dec 2024(Sat-Sun) Weekend Batch

About the Author

Senior Consultant Analytics & Data Science

Sahil Mattoo, a Senior Software Engineer at Eli Lilly and Company, is an accomplished professional with 14 years of experience in languages such as Java, Python, and JavaScript. Sahil has a strong foundation in system architecture, database management, and API integration.