Topic 1.3
UML / Class Relationships
In one line
Interviews rarely ask you to draw formal UML, but the relationship vocabulary (1:1, 1:N, N:N, association, aggregation, composition, inheritance, dependency) is used constantly to describe a class diagram out loud.
Think of it like this
A family tree for code. It shows who owns whom (a House owns its Rooms), who just knows whom (a Doctor knows a Patient), and who is a kind of whom (a Dog is an Animal).
Key ideas
- 01
1:1 — one instance references exactly one other (Customer ↔ Passport).
- 02
1:N — one references many (Order 1—many LineItem). The 'many' side holds the FK.
- 03
N:N — both sides hold collections (Student ↔ Course) via a join/associative table.
- 04
Association: a structural reference, either direction, no ownership (Doctor → Patient).
- 05
Aggregation: whole/part, parts can outlive the whole (Team ▷ Player).
- 06
Composition: whole/part with ownership and life-cycle coupling (House ◇ Room; house dies, rooms die).
- 07
Inheritance: 'is-a', drawn as an empty triangle arrow to the parent.
- 08
Dependency: dashed arrow meaning 'uses briefly' (method parameter, local variable).
- 09
When describing your LLD out loud, always name the multiplicity and the ownership.
Java / Spring map
- →
Composition → private final field constructed inside the owner.
- →
Aggregation → field injected from outside (Spring constructor injection).
- →
Association → a reference field or method parameter with no life-cycle claim.
- →
Dependency → usage inside a method body:
new Receipt(price).
Explain without notes
Draw Parking Lot: Spot belongs to Floor (composition), Ticket references Spot (association). Name each arrow.
Practice
Model a Library: Book, Member, Loan. Identify the multiplicities and whether each link is composition, aggregation, or association.
Completion checklist
I can draw and explain all five relationship arrows with multiplicity.