The Problem It Distinguishes
A team has players. Disband the team — do the players die? Obviously not; they sign elsewhere. But a house’s rooms do cease existing with the house. Both are “whole–part” relationships, yet their lifecycle ownership differs fundamentally — and that difference determines deletion logic, copy semantics, and database cascades. Aggregation is the weak variant: whole holds parts, but parts exist independently.
Team Player
┌──────┐ hollow diamond ┌────────┐
│ Team │◇──────────────────────│ Player │
└──────┘ (shared, independent) └────────┘
team disbands → players live on,
possibly joining another team
The hollow diamond sits on the whole end. Its emptiness is the notation for “holds a reference but does not own the lifetime.”
Java Mechanics
class Team {
private final List<Player> players;
// parts created OUTSIDE and handed in — team doesn't manufacture them
Team(List<Player> players) { this.players = new ArrayList<>(players); }
void disband() {
players.clear(); // release references only;
} // Player objects remain valid elsewhere
}
The tell is in construction: aggregation receives its parts (constructor injection); composition would create them internally (this.room = new Room()).
Where It Appears
- Department ↔ Employees: reorganize departments, employees persist.
- Playlist ↔ Songs: delete playlist, songs stay in library.
- Cache ↔ cached objects: cache eviction never implies object destruction — owners elsewhere still hold them.
- Course ↔ Students: a course enrollment ends; students continue.
The Shared-State Hazard
Because parts are shared, mutation is visible to every holder:
Player p = new Player("Rohit");
teams.get(0).add(p);
teams.get(1).add(p); // same instance in two teams
p.setInjured(true); // BOTH teams observe it instantly
This is correct when shared truth is intended (injury status), a bug when independence was assumed. Value-object copies or defensive duplication avoid unintended coupling.
Aggregation vs Composition vs Association
| Association | Aggregation | Composition | |
|---|---|---|---|
| Phrase | knows-a | has-a | part-of |
| Lifetime link | none | part outlives whole | part dies with whole |
| Part creation | anywhere | external, injected | internal to whole |
| UML | plain line | hollow diamond ◇ | filled diamond ◆ |
Interview Signals
- The discriminating question is always lifecycle: “if I delete the container, must the contents die?” Yes → composition. No → aggregation.
- Copy semantics follow: aggregating structures copy shallowly (references shared) by design; composing structures need deep copies.
- Database translation: aggregation ≈ FK without
ON DELETE CASCADE; composition ≈ cascade delete.
Premium Content
Unlock Aggregation and all premium lessons with a subscription.
From ₹199.99/year — See plans