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.

Beginner 2 min readLesson 3 of 24

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

RelationshipMeaningNotationExample
DependencyUses temporarily (parameter, local variable)Dashed arrow - - ->OrderService uses a Clock passed to a method
AssociationKnows about / holds a referenceSolid line ——>Student — Course
AggregationHas-a, parts can exist independentlyHollow diamond ◇——Team ◇— Player
CompositionOwns; parts die with the wholeFilled diamond ◆——House ◆— Room
Inheritance (generalisation)Is-aHollow triangle ——▷Dog —▷ Animal
RealisationImplements an interfaceDashed line, hollow triangle - - ▷ArrayList - -▷ List

From weakest to strongest coupling: dependency → association → aggregation → composition → inheritance.

inheritsimplementsowns 0..*◆ composed of 1..*lives inuses«abstract» AnimalDog«interface» PetOwnerHouseRoomVet

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
}

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.

UserControllerOrderServicePaymentGatewayRepositoryPOST /ordersplaceOrder(cart)charge(total)receiptsave(order)order201 Created

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.
Key takeaways
  • 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.