100% Free Forever
AI-Powered Learning
Industry Expert Content
Certificates & Badges
Learn At Your Own Pace

Aggregation vs Composition in OOP

Learn the difference between aggregation and composition in OOP, object lifecycle ownership, and how each maps to UML diagrams.

mediumQ21 of 226 in Object Oriented Programming Est. time: 6 minsLast updated:
Open Code Lab

Expected Interview Answer

Aggregation and composition are both "has-a" relationships between objects, but composition implies strict ownership where the part cannot outlive the whole, while aggregation is a looser association where the part can exist independently.

In composition, the contained object’s lifecycle is fully managed by the containing object — when the container is destroyed, its parts are destroyed with it, such as a House owning its Rooms. In aggregation, the contained object is merely referenced by the container and could be shared or reused elsewhere, such as a Department having Employees who continue to exist even if the department is dissolved. This distinction matters for memory management, object lifecycle design, and how tightly two classes should be bound together, and it directly affects UML modeling, where composition is drawn with a filled diamond and aggregation with a hollow diamond.

  • Clarifies true object ownership and lifecycle responsibility
  • Prevents accidental shared-state bugs from misjudged ownership
  • Improves reusability when parts genuinely need independent lifecycles
  • Maps directly to UML modeling and clean domain design

AI Mentor Explanation

A cricket ball assigned to a specific match is composition — once the match ends and the ball is retired, it has no independent existence relevant to that fixture, it was created for and dies with that game context. A player on a team, by contrast, is aggregation — the player exists before joining the squad and continues playing cricket even if that particular team is disbanded, so the team merely references the player rather than owning their existence.

Step-by-Step Explanation

  1. Step 1

    Identify the relationship

    Determine if one class logically "has" instances of another as parts or members.

  2. Step 2

    Ask the lifecycle question

    Would the part still make sense to exist if the whole were destroyed right now?

  3. Step 3

    Model composition when tightly owned

    If the answer is no, implement the part as created and destroyed by the container, often instantiated inside its constructor.

  4. Step 4

    Model aggregation when shared/independent

    If the answer is yes, pass the part in via constructor/setter as a reference the container does not own.

What Interviewer Expects

  • A clear articulation of the lifecycle/ownership distinction
  • Correct UML notation: filled diamond for composition, hollow for aggregation
  • Concrete has-a examples for both (e.g. House-Room vs Department-Employee)
  • Discussion of how this affects constructor design and object cleanup

Common Mistakes

  • Treating aggregation and composition as identical concepts
  • Confusing "has-a" (aggregation/composition) with "is-a" (inheritance)
  • Getting the UML diamond notation backwards
  • Not mentioning the lifecycle/ownership distinction as the core difference

Best Answer (HR Friendly)

Composition is when one object fully owns another, so the part is created and destroyed along with the whole, like a car and its engine. Aggregation is a looser relationship where the part exists independently and is just associated with the whole, like a university and its students, who exist before and after enrollment.

Code Example

Composition vs aggregation
// Composition: Engine is created and owned by Car
class Engine {}

class Car {
    private final Engine engine = new Engine(); // owned, dies with Car
}

// Aggregation: Employee exists independently of Department
class Employee {}

class Department {
    private final List<Employee> employees;
    Department(List<Employee> employees) {
        this.employees = employees; // referenced, not owned
    }
}

Follow-up Questions

  • How would you represent aggregation versus composition in a UML diagram?
  • How does this distinction affect garbage collection in Java?
  • Can a relationship be composition in one context and aggregation in another?
  • How does composition relate to the "favor composition over inheritance" principle?

MCQ Practice

1. In composition, when the container object is destroyed, the contained object?

Composition implies strict ownership — the part’s lifecycle is bound to the whole.

2. In UML, aggregation is drawn with?

Aggregation uses a hollow (unfilled) diamond; composition uses a filled diamond.

3. A Department having a list of Employee references who exist independently is an example of?

Employees exist and can be reassigned independently of any one department, making this aggregation.

Flash Cards

What is composition?A strict "has-a" relationship where the part is owned by and shares the lifecycle of the whole.

What is aggregation?A looser "has-a" relationship where the part exists independently of the whole.

How is composition drawn in UML?With a filled diamond at the container end.

Give an example of aggregation.A Department referencing Employees who continue to exist if the department is dissolved.

1 / 4

Continue Learning