In Java Association, Aggregation and Composition Explained

In Java, aggregation is a type of association between classes in which one class (the whole or container) contains a reference to another class (the part or component). Aggregation represents a “has-a” relationship, where one class contains objects of another class as part of its internal structure. It is a way to represent the relationship between objects when one object is composed of or contains other objects.

Relationships between classes are very important in object-oriented programming paradigm. The classes and OOP are built to model real-world entities. And the relationships between classes has been introduced to model the connections between real-world entities, which are mapped to classes.

Forms of Relationships

There are 2️ main forms of relationships in OOP paradigm.

  1. IS-A (Inheritance)
  2. HAS-A (Association)

IS-A Relationship

This is exactly Inheritance in OOP. That is parent — child communication. Let’s take some examples…

  • Car IS A Vehicle
  • Cat IS AN Animal
  • Manager IS AN Employee

All these examples denote that sub type is in the form of super type creating is-a relationship.

HAS-A Relationship

This means the relationship between 2 entities/objects in real world which simply called Association. Let’s take some examples…

  • Hotel HAS Rooms
  • School HAS Departments
  • Employee HAS Address

This represents the ownership of an entity in another. One entity belongs to another…

There are 2️ forms of Association that are possible in Java:

a) Aggregation — loose coupling, weak

b) Composition — tight coupling, strong

Aggregation

Aggregation is typically used to model scenarios where one class represents a larger entity that is composed of smaller, reusable components. These components can exist independently and may be shared among multiple instances of the container class.

Here’s an example of aggregation in Java:

class Department {
    private String name;

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

    // Other department-related methods
}

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

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

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

    // Other university-related methods
}

In this example:

  • The University class has a list of Department objects, representing the departments within the university.
  • The University can add, remove, or modify departments, but the departments themselves are independent entities that can exist outside the university.

Aggregation is useful for creating complex objects by combining simpler components. It allows for code reuse and better organization of classes in object-oriented programming.

Composition:

In Java, composition is a design principle that represents a “whole-part” relationship between classes. Unlike aggregation, which is a weaker form of association, composition implies a stronger relationship where one class (the whole) is composed of other classes or objects (the parts), and the parts cannot exist independently outside the whole. In other words, when the whole is destroyed, its parts are also destroyed.

Composition is often used when one class is closely related to another class in such a way that one class is an essential part of the other. It allows you to create complex objects by assembling simpler objects as their components.

Here’s an example of composition in Java:

class Room {
    private String name;

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

    // Other room-related methods
}

class House {
    private List<Room> rooms; // Composition

    public House() {
        this.rooms = new ArrayList<>();
        rooms.add(new Room("Living Room"));
        rooms.add(new Room("Bedroom"));
        rooms.add(new Room("Kitchen"));
    }

    public List<Room> getRooms() {
        return rooms;
    }

    // Other house-related methods
}

In this example:

  • The House class creates and owns a list of Room objects as its parts.
  • Rooms are created and managed by the house, and they cannot exist independently outside the house. If the house is destroyed, the rooms are also destroyed.

Composition is a powerful way to model complex systems, as it allows you to create hierarchies of objects with clearly defined ownership relationships. When using composition, the lifetime of the parts is tied to the lifetime of the whole, ensuring that parts are properly managed and encapsulated within the whole object.

Difference between Aggregation and Composition:

Aggregation and composition are two different types of relationships between classes in object-oriented programming. They represent how one class is related to another class or how objects of one class are used within another class. Here are the key differences between aggregation and composition:

Aggregation:

1. Strength of Relationship:

  • Aggregation represents a weaker relationship between classes.
  • It implies a “has-a” or “uses-a” relationship, where one class contains a reference to another class, but the objects involved can exist independently.

2. Independence:

  • In aggregation, the “part” class (the one contained within the other) can exist outside of the “whole” class (the one containing the part).
  • The part can have a longer lifespan than the whole, and it may be used by multiple instances of the whole class.

3. Ownership:

  • There is no ownership or strong encapsulation in aggregation. The part does not belong exclusively to the whole.
  • If the whole object is destroyed, the part objects can still exist.
  • Example: An example of aggregation could be a university containing departments. Departments can exist independently and can be part of multiple universities.

Composition:

  1. Strength of Relationship:
  • Composition represents a stronger relationship between classes.
  • It implies a “whole-part” relationship, where one class (the whole) is composed of other classes (the parts), and the parts cannot exist independently outside of the whole.

2. Independence:

  • In composition, the “part” objects are created and managed by the “whole” class.
  • The parts cannot exist without the whole, and their lifecycles are tightly bound to the whole object.

3. Ownership:

  • Composition implies strong ownership and encapsulation. The part objects are owned and managed exclusively by the whole class.
  • When the whole object is destroyed, the part objects are also destroyed.
  • Example: An example of composition could be a house containing rooms. Rooms are created and managed by the house, and they cannot exist independently. When the house is destroyed, the rooms cease to exist.

In summary, the main difference between aggregation and composition lies in the strength of the relationship and ownership. Aggregation represents a weaker, more independent relationship, while composition represents a stronger, ownership-based relationship where the parts are tightly integrated with the whole. The choice between these two relationships depends on the specific design requirements of the software.

Conclusion

What is the difference between Association and Aggregation?

Association means the relationship between the two interdependent classes. The connection is established using the two objects. Through association, a class can use another class component. Whereas, Aggregation is a particular case of Association in which the two classes are related to each other, and they follow a whole and part relationship.

What is the difference between Association and Composition?

Association means to specify a relationship between two classes using their objects. It allows multiple types of relationships like one-to-one, one-to-many, many-to-one, and many-to-many. In contrast, Composition is a restricted Aggregation, which means as per aggregation, it allows the relationship between the two classes, but the objects are dependent on each other. In Composition, only a one-to-one relationship is valid.

What is Aggregation and composition in Java with real life example?

In Java, aggregation represents a “has-a” relationship, where one class contains another class as part of its structure, but they can exist independently. Example: A university “has” departments. Composition is a stronger relationship, where one class owns another class, and the owned class cannot exist independently. Example: A car “has” an engine.

Leave a Comment