UML diagrams: the visual language of software design
UML is how engineers communicate design at a whiteboard. Learn class diagrams, sequence diagrams, state machines, and component diagrams with real Java examples.
UML stands for Unified Modeling Language. It is a standard visual notation for communicating software structure and behavior, and it has been the working language of software design since the late 1990s. You do not need to memorize every UML diagram type. In practice and in interviews, four types cover 95% of what you will ever need.
In interviews, drawing on a whiteboard IS UML. The moment you sketch a box labeled OrderService with an arrow to PaymentGateway, you are doing UML. Interviewers expect you to draw those boxes and arrows fluently, label every relationship, and explain what you mean. The candidates who stand out are the ones who draw instinctively, not the ones who ask "should I draw a diagram?"
| Diagram | What it shows | When you use it |
|---|---|---|
| Class Diagram | Static structure: classes, relationships, fields, methods | System structure, LLD interview design |
| Sequence Diagram | Time-ordered message flow between objects | API flows, protocol design, checkout flows |
| State Diagram | States and transitions of a single object | Order lifecycle, connection states, subscription status |
| Component Diagram | High-level module dependencies | Architecture overview, microservice sketch |
I reach for a class diagram first in any LLD interview. It forces me to name the entities, define relationships, and expose coupling before writing a single line of code.
Class Diagrams
Class diagrams are the foundation of LLD design. A well-drawn class diagram communicates the entire structure of a system before any code exists.
Building Blocks
A class box has three compartments: name at the top, fields in the middle, methods at the bottom. Visibility modifiers prefix every member:
+public-private#protected~package-private
Interfaces use the <<interface>> stereotype. Abstract classes use <<abstract>> or render the class name in italics. In Mermaid's classDiagram syntax, use <<interface>> and <<abstract>> annotations.
Relationships (Most Candidates Get This Wrong)
The six relationship types are where most candidates stumble. They use inheritance for everything, or they draw arrows without knowing what type they are.
| Relationship | Arrow | Meaning | Java keyword | Strength |
|---|---|---|---|---|
| Dependency | ..> dashed open | Uses temporarily | method param, local var | Weakest |
| Association | --> solid open | Has a reference | field | Weak |
| Aggregation | o--> open diamond | Owns but parts survive independently | field, collection | Medium |
| Composition | *--> filled diamond | Owns, parts destroyed with owner | field, inner instance | Strong |
| Realization | `.. | >` dashed closed | Implements interface | implements |
| Generalization | `-- | >` solid closed | Extends class | extends |
Aggregation vs Composition is the question I hear most often in interviews. The answer is one question: "Can the part exist without the whole?"
UniversityaggregatesDepartment. A department can exist independently (it could be transferred to another university, or outlive a reorganization). The diamond is open.HousecomposesRoom. A room without a house is not a room. When the house is torn down, the rooms go with it. The diamond is filled.
In Java, neither aggregation nor composition has a keyword. The difference lives in intent and lifecycle management. If you manage the part's creation and destruction inside the enclosing class, it is composition. If the part is passed in and could outlive the owner, it is aggregation.
All Six Relationships in One Diagram
Notice: Order composes OrderItem (items cannot exist outside an order), but aggregates Discount (a discount code can exist independently and apply to many orders). Order depends on Payment as a temporary use at checkout.
Multiplicity
Multiplicity tells you how many instances participate on each end of an association:
| Notation | Meaning |
|---|---|
1 | Exactly one |
0..1 | Zero or one (optional) |
* | Zero or many |
1..* | One or many |
2..5 | Specific range |
A Customer has 0..* orders. An Order has 1..* items. An Order ships to exactly 1 address. These constraints are design decisions, not afterthoughts.
Complete Example: Library Management System
This is the kind of class diagram I would draw in a 45-minute LLD interview for "design a library system."
Library composes Book (the books belong to this library instance). Member composes LibraryCard (a card without a member is meaningless). Book is associated with Author (authors exist independently). Loan is associated with both Member and Book.
Now here is how the key classes map directly to Java:
// Maps to the Book node in the class diagram.
// availableCopies is managed by Library, not by Book itself,
// so checkout/return logic stays in Library (single responsibility).
public class Book {
private final String isbn;
private final String title;
private int totalCopies;
private int availableCopies;
public Book(String isbn, String title, int totalCopies) {
this.isbn = isbn;
this.title = title;
this.totalCopies = totalCopies;
this.availableCopies = totalCopies;
}
public boolean isAvailable() {
return availableCopies > 0;
}
// Package-private: only Library should manipulate copies
void decrementAvailable() {
if (availableCopies <= 0) throw new IllegalStateException("No copies available");
availableCopies--;
}
void incrementAvailable() {
if (availableCopies >= totalCopies) throw new IllegalStateException("All copies already returned");
availableCopies++;
}
public String getIsbn() { return isbn; }
public String getTitle() { return title; }
public int getAvailableCopies() { return availableCopies; }
}Sequence Diagrams
Sequence diagrams show what happens over time between components. Where class diagrams show static structure, sequence diagrams show dynamic behavior. In interviews, I draw a sequence diagram whenever the interviewer asks "walk me through how this request flows."
Building Blocks
- Participants: Named boxes at the top. Can be actors (users) or objects (services).
- Lifelines: Vertical dashed lines hanging from each participant.
- Synchronous messages: Solid arrow with filled arrowhead. Caller waits for response.
- Asynchronous messages: Solid arrow with open arrowhead. Caller does not wait (fire and forget, Kafka produce).
- Return messages: Dashed arrow. Always label these.
- Activation boxes: Thin rectangles on the lifeline showing when an object is active.
- Alt/opt/loop/par frames: Rectangles with a label in the top-left corner for conditional and repeating logic.
Alt Frame (If-Else Branching)
The alt frame is your if-else. Label both branches. Never draw a happy path without an error path in an interview.
Loop Frame
Complete Example: User Places an Order
This is the kind of end-to-end sequence I draw when asked "how does order placement work?"
The dashed --) arrow to NotificationService is async (fire and forget). The notification is sent to a queue; order placement does not wait for it. Getting this right in an interview shows you understand async boundaries.
Common Sequence Diagram Mistakes
| Mistake | Why it matters | Fix |
|---|---|---|
| Return arrows with no label | Reviewer cannot tell what was returned | Label every dashed return arrow |
| Showing internal class calls | Sequence diagrams show WHAT flows between services | Only draw cross-service messages |
| Missing async boundaries | Queue producers look like synchronous calls | Use --) or annotate [async] |
| No error paths | Happy path only misses half the design | Add alt frames for failure paths |
| Too many participants | 8+ participants are unreadable | Break into two diagrams at a natural boundary |
State Diagrams
State diagrams model the lifecycle of a single object. Every domain object that can be in multiple states and transitions between them under specific conditions is a candidate for a state diagram. I draw these the moment an interviewer describes an entity with a "status" field.
Building Blocks
- States: Rounded rectangles. Each state is a stable condition the object can be in.
- Initial state: Filled circle. Every state machine has exactly one.
- Final state: Circle with a ring around it. May have multiple.
- Transitions: Arrows between states. Label format:
event [guard] / action.event: what triggers the transition[guard]: optional condition that must be true/action: what happens during the transition
- Entry/exit actions: Side effects that happen every time you enter or leave a state.
Complete Example: Order Lifecycle
Every arrow has a label. The guard conditions (e.g. all_items_in_stock, within_30_days) are embedded in the transition label to show the condition that must be true for the transition to fire. This diagram tells you immediately that cancellation after processing starts requires a different flow.
Mapping to the State Pattern in Java
This state diagram maps directly to the State pattern. Each state node becomes a class implementing a OrderState interface.
// Each box in the state diagram becomes one implementation of this interface.
// The order object delegates all state-dependent behavior to the current state.
public interface OrderState {
void confirm(Order order);
void startProcessing(Order order);
void ship(Order order);
void deliver(Order order);
void cancel(Order order);
void requestReturn(Order order);
String getStateName();
}The state diagram and the code are isomorphic. Each state node is a class. Each arrow is a method call. Each guard is a condition inside that method. When a product manager describes a new status transition, you can trace it straight to the correct state class without hunting through a chain of if-else blocks.
When to Draw State Diagrams
Draw a state diagram when the object has:
- A named "status" field that drives behavior
- Multiple transitions with guards (not just linear: A to B to C)
- Behavior that differs depending on which state the object is in
Common candidates:
- User-facing workflows: order, subscription, booking, ticket
- Protocol implementations: TCP socket (CLOSED / SYN_SENT / ESTABLISHED / FIN_WAIT / CLOSED)
- Resource lifecycle: document (DRAFT / REVIEW / PUBLISHED / ARCHIVED)
Component Diagrams
Component diagrams operate at a higher level than class diagrams. Where class diagrams show individual classes, component diagrams show modules, services, and the dependencies between them. I use these in architecture discussions, not in LLD sessions.
A component diagram answers: "Which service calls which?" It exposes coupling at the service level so you can spot circular dependencies and identify which services need to be deployed together.
Notice NotificationService has no inbound arrows from other services in the business logic layer. It only receives events from OrderService via an async channel. This is a deliberate design: notification delivery details (email vs SMS vs push) do not belong in order processing.
When to use component vs class diagram
Use a component diagram for architectural conversations: "how do these services relate?" Use a class diagram for design conversations: "how do these classes relate?" If your interviewer asks you to sketch the system before diving into LLD, start with a component diagram, then drill into the class diagram for the most complex subsystem.
How to Draw UML in an Interview
The whiteboard is where UML lives in interviews. The marker-in-hand skill is separate from the knowledge skill. Practice both.
The Whiteboard Protocol
-
Ask what level of detail the interviewer wants. "Should I go full class diagram with all methods, or start with a high-level entity sketch?" Some interviewers want details, others want to see how you decompose problems. Their answer tells you which to start with.
-
Start with main entities as boxes. Write the class name in the center. Do not add fields or methods yet. Get the landscape of entities on the board first so you can see all the relationships.
-
Add relationships with arrows as you discover them. Draw the arrow, then decide what type it is. Ask yourself: is this HAS-A (composition/aggregation/association) or IS-A (generalization)?
-
Label every arrow. Never leave an arrow unlabeled. At minimum, write the relationship verb ("places", "contains", "implements"). Add multiplicity for associations.
-
Add multiplicities to all associations.
Customer 1 places 0..* Orderis infinitely clearer than a naked arrow. -
Use
<<interface>>for abstractions. Any time you hear "we might need different implementations" from the interviewer, that is your cue to draw an interface. -
Talk while drawing. "I am making
Paymentan interface here because we will have Stripe, PayPal, and internal credit implementations. This keepsOrderServicedecoupled from any specific gateway."
What Interviewers Look For
- Correct IS-A vs HAS-A. Overusing inheritance is the most common mistake. If
OrderProcessordoes not truly specializeProcessor, use composition, not inheritance. - Interface extraction for dependencies. If
OrderServicedepends directly onStripeClient, the interviewer notes the coupling. ExtractPaymentGatewayas an interface. - Correct multiplicity. A
Customerhas MANY orders over time, not one. Getting multiplicity wrong suggests weak domain modeling. - Separation of concerns. Service classes separate from model classes. No god class with 20 arrows pointing at it.
- Naming. Use domain terms, not pattern jargon.
PaymentGatewayis better thanIPaymentStrategy.OrderRepositoryis better thanDataAccessObject.
Common Interview UML Mistakes
| Mistake | Why it matters | Fix |
|---|---|---|
| Composition for everything | Ignores independence of related objects | Ask "can this exist without its parent?" |
| Drawing methods on every class upfront | Clutters the diagram before you understand behavior | Start with names only, add methods when discussing behavior |
| No multiplicity on associations | No multiplicity means no clarity about data volume | Always label associations with 1, *, or 0..1 |
| God class: everything arrows to one class | Shows no thought about coupling | Ask "which service owns this behavior?" and redistribute |
| Inheritance when interface fits | Overuse of IS-A for HAS-A relationships | Use generalization only for true specialization |
| Unlabeled arrows | Reviewer cannot interpret the diagram | Every arrow has a label, always |
The coupling smell
If your class diagram shows 6+ arrows pointing INTO one class, that is a coupling smell. It means too much logic is concentrated in one place. In an interview, pause and say: "I notice OrderService is getting too many dependencies. I would split it by considering which of these behaviors could live in a separate, more focused service."
UML Tools
| Tool | Best for | Input style |
|---|---|---|
| Mermaid | Code-adjacent docs, pull request diagrams | Text in Markdown, GitHub renders natively |
| PlantUML | Complex UML with full spec support, large diagrams | Text-based, needs a render server |
| Draw.io / Excalidraw | Whiteboard-style, free-form, hand-drawn | GUI drag-and-drop |
| Lucidchart | Team collaboration, enterprise docs | SaaS GUI |
| Whiteboard + marker | Interviews | Muscle memory required |
Practice drawing by hand
For interviews, practice drawing with a physical marker on a whiteboard or paper. The muscle memory of sketching boxes, arrows, and stereotypes quickly is a real, learnable skill. Candidates who draw fluidly while talking look like they think in design. Candidates who pause to think about how to draw an arrow look uncertain.
Test Your Understanding
Quick Recap
- Four diagram types cover 95% of interviews: class, sequence, state, component. Master these four, ignore the rest.
- Six class diagram relationships in order of strength: dependency (weakest), association, aggregation, composition, realization, generalization. Know the difference between aggregation and composition by asking "can the part survive without the whole?"
- Every arrow needs a label. Unlabeled arrows are unfinished thinking. In a diagram, as on a whiteboard, every relationship needs a name.
- Multiplicity is not optional.
Customer 1toOrder 0..*is design.CustomertoOrderwith no multiplicity is a sketch. - Sequence diagrams need alt frames. A happy-path-only sequence diagram misses the design. Always add the failure branch.
- State diagrams map directly to the State pattern. Each node is a class. Each arrow is a method. The diagram is the design.
- In interviews, draw while talking. Narrate every decision. "I am making this an interface because..." is exactly what interviewers want to hear.