OOPS Interview Questions · Question 14

What is the difference between association, aggregation, and composition?

Interview preparation resource from Gate Smashers.

Interview-ready answer

Association is a general relationship in which objects are connected or interact, without implying ownership. Aggregation and composition are whole–part forms of association. In aggregation, parts can exist independently of the whole and may be shared. In composition, the whole exclusively owns its parts in the model, and a part’s lifetime is tied to that whole.

Association (Java)java
class Course {
  private final String title;

  Course(String title) {
    this.title = title;
  }

  String getTitle() {
    return title;
  }
}

class Student {
  private final String name;

  Student(String name) {
    this.name = name;
  }

  void enroll(Course course) {
    System.out.println(name + " enrolls in " + course.getTitle());
  }
}
Aggregation (Java)java
import java.util.List;

class Player {
  private final String name;

  Player(String name) {
    this.name = name;
  }
}

class Team {
  private final List<Player> players;

  Team(List<Player> players) {
    this.players = players;
  }
}

// Player objects are supplied externally and can exist without a Team.
Composition (Java)java
import java.util.ArrayList;
import java.util.List;

class House {
  private static class Room {
    private final String name;

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

  private final List<Room> rooms = new ArrayList<>();

  House(int count) {
    for (int i = 0; i < count; i++) {
      rooms.add(new Room("Room" + i));
    }
  }
}

// Room is an internal part of House in this model.
Understand it clearly

Association

Association represents a general structural relationship between classes or objects. For example, a Student may be associated with a Course. Neither object necessarily owns or controls the lifecycle of the other.

A temporary use of an object, such as passing it to a method, is often modeled as a dependency rather than a persistent association in UML. In ordinary code, both may be implemented with references, so the intended domain relationship matters more than the syntax.

Aggregation

Aggregation is a whole–part relationship with independent lifecycles. The whole groups or refers to parts, but the parts can continue to exist if the whole is removed and can potentially be shared by other wholes.

For example, a Team aggregates Player objects when players exist independently of a particular team. Aggregation does not imply that the whole is responsible for creating or destroying its parts.

Composition

Composition is the strongest whole–part relationship. A part belongs to one composite at a time in the model, and its lifecycle is dependent on that composite. When the composite is destroyed, its parts are conceptually destroyed as well.

Composition is a design relationship, not merely a coding pattern. Creating a member object inside a constructor can support composition, but it does not by itself establish the relationship; the model must also treat that object as an exclusive, lifecycle-bound part.

Practical interpretation

Use association when objects simply collaborate. Use aggregation when the whole groups independent parts. Use composition when a part has no meaningful independent lifecycle outside its whole.

The exact implementation of ownership and cleanup depends on the language and runtime. For example, garbage-collected languages reclaim memory automatically, but the composition relationship still expresses lifecycle and exclusive ownership in the object model.

  • UML notation: Association is typically shown with a plain line, aggregation with a hollow diamond at the whole end, and composition with a filled diamond at the whole end.
Quick comparison
BasisRelationshipMeaning and lifecycle
AssociationGeneral connection or collaboration between objectsNo ownership or lifecycle dependency is implied.
AggregationWhole–part relationship with independent partsParts can outlive the whole and may be shared or reassigned.
CompositionWhole–part relationship with exclusive, lifecycle-bound partsA part belongs to its composite and is conceptually removed with it.