Hirestack
Hirestack
Chapter 4

Class relationships and notation

📖 2 min read · 3 sections · May 2026

Class diagrams are the lingua franca of LLD. You don't need full UML — but you should be fluent in the basics.

The five relationships

RelationshipSymbolMeaningExample
Inheritance ("is-a")extends / arrow with triangleSubclass IS-A parentCar extends Vehicle
Composition ("contains, owns")Solid diamondWhole owns part; part lifecycle tied to wholeCar has Engine (Engine ceases when Car is destroyed)
Aggregation ("has, but doesn't own")Hollow diamondWhole contains part; part can exist independentlyDepartment has Employees (Employees exist beyond Department)
Association ("uses, knows about")Plain arrowOne class refers to anotherOrder has-a Customer reference
Dependency ("depends on")Dashed arrowTemporary use (parameter, local variable)OrderService.process(Payment p) — uses Payment

Composition vs Aggregation in practice

This distinction matters because it affects ownership semantics:

In code, both look similar (a field holding a reference). The distinction is semantic: who's responsible for the lifecycle? In most real systems, the answer is "they outlive different things at different rates" — aggregation is the default.

Inheritance vs Composition

"Favor composition over inheritance" — Gang of Four

Inheritance is the strongest form of coupling. Subclass inherits everything — fields, behavior, contracts. Changing the parent class can silently break the subclass.

Composition is flexible. A class composed of other classes can swap them at runtime, mock them for tests, and evolve independently.

Use inheritance when:

Use composition (with interfaces) for everything else.