Class Diagram
The UML picture of a design at rest: the classes, what they hold and do, and how they relate. In an LLD interview it is the main thing you draw.
Overview
A class diagram shows the static shape of a design: which classes exist, what data and operations each one has, and how they connect. It does not show time or order; that is what sequence and state diagrams are for. The notation comes from UML 2.5.1, published by the Object Management Group in December 2017; the symbols and arrowheads below follow that specification, and the interview advice is our own.
In a low level design interview, the class diagram is your primary artifact. It turns a vague prompt such as 'design a library system' into named classes with clear responsibilities, and it gives the interviewer something concrete to question. The rest of the interview usually follows its lines: each relationship becomes a field, each operation becomes a method, and each hard question is about one arrow.
The class box
A class is a rectangle split into compartments. The name is at the top; the attributes compartment comes next and the operations compartment below it. The attribute and operation compartments can be left out. An abstract class is shown with its name in italics, and an interface uses the same box with the keyword «interface» above the name.
Each member starts with a symbol for its visibility, which says who may use it.
- Public (+): any class may use it.
- Private (-): only the class itself.
- Protected (#): the class and its subclasses.
- Package (~): classes in the same package.
An attribute is written as visibility, name, a colon and the type, for example -balance: Decimal. An operation adds a parameter list and a return type, for example +withdraw(amount: Decimal): bool.
A BankAccount box uses all four. Its attributes are -id: UUID and -balance: Decimal (private), #overdraftLimit: Decimal (protected) and ~lastAudit: DateTime (package). Its public operations are +deposit(amount: Decimal), +withdraw(amount: Decimal) bool and +balance() Decimal, and -assertPositive(amount: Decimal) is a private helper. The last operation, +nextId()$ UUID, is static: UML underlines static members, and Mermaid marks them with a trailing dollar sign.
Interview questions
- What is the difference between aggregation and composition, and how does each look in code?
- How do you show an interface, an abstract class and the Strategy pattern on a class diagram?
- Translate this class diagram into code.
- What should you include and what should you leave out of an interview class diagram?
- When is a line a dependency rather than an association?
Sign up free to read the full topic
More Low-Level Design topics
Start free on Cariara · Coding problems · System design studio