Low-Level Design · Note 2 of 9
UML Class Diagrams for LLD Interviews
Quick answer
What is a UML class diagram?
A UML class diagram shows the classes of a design, their fields and methods, and the relationships between them. Each class is a box with three compartments: name, fields and methods, each member prefixed with its visibility. Lines between boxes show association, aggregation, composition, inheritance, realization and dependency, with multiplicity such as 1 or 0..* at the ends.
On this page
A class diagram is the shared sketch of an LLD round: who the classes are, what they hold, and how they relate. Learn the notation once and you can draw any design in minutes.
The class box
- Three compartments: name, fields, methods.
- A member is
visibility name: type; a method adds(params): returnType. - Visibility:
+public,-private,#protected,~package. - Interfaces and abstract classes carry a stereotype:
«interface»,«abstract».
A class box starts with its name in the top compartment. An abstract class or interface adds a stereotype such as «interface» above the name.
Transcript · 4 steps
The six relationships
| Relation | Line | Means |
|---|---|---|
| Association | solid, open arrow | holds a reference |
| Aggregation | hollow diamond at whole | has parts that live on |
| Composition | filled diamond at whole | parts die with the whole |
| Inheritance | solid, hollow triangle | is a kind of |
| Realization | dashed, hollow triangle | implements an interface |
| Dependency | dashed, open arrow | uses briefly |
Association: solid line, open arrow. Read it as "a member knows the books it has".
Transcript · 6 steps
Multiplicity
- Written at each end of a line: how many objects sit there.
1exactly one,0..1optional,0..*or*any,1..*at least one,0..3a range.- Read across the line: "one Member has 0..3 Loans".
- It is a rule: the code must refuse what the diagram forbids.
Multiplicity is written at each end of a line: one Member has 0..3 Loans, and each Loan belongs to exactly 1 Member.
Transcript · 5 steps
A worked example: a library
Requirement: members join a library and borrow its books, at most three at a time; overdue loans are fined.
The library example starts from Library, the class the use cases enter through: members join it and borrow its books.
Transcript · 6 steps
- Aggregation, not composition, where parts outlive the whole.
- An interface (
Borrowable) at the point where new kinds will arrive. - A dependency for helpers that keep no reference.
Drawing it in an interview
- Start with names only; add fields and methods as use cases need them.
- Draw only what you will discuss: skip getters, setters and constructors.
- Put multiplicity on the lines that carry a business rule.
- When unsure between aggregation and association, say why you chose; reasoning matters more than notation.
Interview questions
- Aggregation or composition for a car and its engine? Composition if engines are never moved between cars in this system; aggregation if they are tracked and reused.
- Where does the diamond go? On the whole, never the part.
- How do you show an abstract method? In italics, or with
{abstract}; in an interview, the«abstract»stereotype on the class is enough. - **What does 0..* next to Book mean?** Any number of books, including none, at that end.
Next: Design Patterns in LLD · Before this: Introduction to Low-Level Design
Common questions
What do the symbols +, -, # and ~ mean in a UML class diagram?
They give a member's visibility. A plus is public, a minus is private, a hash is protected (the class and its subclasses) and a tilde is package-private. A common default is private fields and public methods.
What is the difference between aggregation and composition in UML?
Both mean a whole has parts. Aggregation, a hollow diamond at the whole, means the parts can exist on their own, like members of a library. Composition, a filled diamond, means the parts are created and destroyed with the whole, like rooms of a house.
What is the difference between inheritance and realization?
Inheritance, a solid line with a hollow triangle, means a class extends another and is a kind of it. Realization, a dashed line with a hollow triangle, means a class implements an interface and promises its methods.
What does multiplicity mean in a class diagram?
Multiplicity says how many objects can sit at one end of a relationship: 1 exactly one, 0..1 optional, 0..* or * any number, 1..* at least one, or a range like 0..3. It is a rule the code must enforce.
When should I use a dependency instead of an association?
Use an association when a class keeps a reference to the other as a field. Use a dependency, a dashed arrow, when it only uses the other briefly, as a parameter, a local variable or a return value.