UML & Class Relationships
Reading and drawing class diagrams — association, aggregation, composition, inheritance, realisation and dependency — plus multiplicity, sequence diagrams, and how to sketch them in interviews.
UML (Unified Modeling Language) is a visual vocabulary for software design. You rarely need all of it; two diagrams cover 90% of practical use: class diagrams (structure) and sequence diagrams (interactions). In low-level design interviews, a quick class diagram is how you communicate your model.
Class boxes
A class is drawn as a box with three compartments: name, attributes, operations. Visibility markers: + public, - private, # protected, ~ package.
┌──────────────────────────┐
│ Account │
├──────────────────────────┤
│ - id: String │
│ - balance: Money │
├──────────────────────────┤
│ + deposit(amt: Money) │
│ + withdraw(amt: Money) │
│ + getBalance(): Money │
└──────────────────────────┘Interfaces are marked «interface»; abstract classes and methods are written in italics.
The six relationships
| Relationship | Meaning | Notation | Example |
|---|---|---|---|
| Dependency | Uses temporarily (parameter, local variable) | Dashed arrow - - -> | OrderService uses a Clock passed to a method |
| Association | Knows about / holds a reference | Solid line ——> | Student — Course |
| Aggregation | Has-a, parts can exist independently | Hollow diamond ◇—— | Team ◇— Player |
| Composition | Owns; parts die with the whole | Filled diamond ◆—— | House ◆— Room |
| Inheritance (generalisation) | Is-a | Hollow triangle ——▷ | Dog —▷ Animal |
| Realisation | Implements an interface | Dashed line, hollow triangle - - ▷ | ArrayList - -▷ List |
From weakest to strongest coupling: dependency → association → aggregation → composition → inheritance.
Aggregation vs composition in code
// Composition: Rooms are created and owned by the House
class House {
private final List<Room> rooms = new ArrayList<>();
House(int n) { for (int i = 0; i < n; i++) rooms.add(new Room()); }
}
// Aggregation: Players exist independently and can switch teams
class Team {
private final List<Player> players = new ArrayList<>();
void add(Player p) { players.add(p); } // created elsewhere
}class House: # composition
def __init__(self, n: int):
self.rooms = [Room() for _ in range(n)]
class Team: # aggregation
def __init__(self):
self.players: list[Player] = []
def add(self, p: "Player"):
self.players.append(p) # Player lives on if the team is disbandedclass House { // composition
private rooms: Room[];
constructor(n: number) { this.rooms = Array.from({ length: n }, () => new Room()); }
}
class Team { // aggregation
private players: Player[] = [];
add(p: Player) { this.players.push(p); }
}The difference is about lifecycle ownership, not syntax: both are fields holding references.
Multiplicity
Written at the ends of an association: 1 (exactly one), 0..1 (optional), * or 0..* (many), 1..* (at least one). "A Customer places 0..* Orders; each Order belongs to exactly 1 Customer."
Sequence diagrams
Show who calls whom, in what order, over time (top to bottom). Solid arrows are calls; dashed arrows are returns.
Other UML diagrams (brief)
- Use case: actors and the goals they achieve with the system.
- Activity: a flowchart of a workflow with decisions and parallel branches.
- State machine: states and transitions of one object (order: created → paid → shipped) — see State pattern.
- Component / deployment: modules and where they run.
- Class diagrams show structure; sequence diagrams show interactions over time.
- Relationships by strength: dependency, association, aggregation, composition, inheritance (plus realisation).
- Aggregation = independent parts; composition = owned parts sharing the whole's lifecycle.
- Multiplicity: 1, 0..1, , 1...
- In interviews, clarity beats notation precision.