UML class diagrams
The structure diagram that carries most of UML's vocabulary: types, their attributes and operations, and the six kinds of line that connect them. Learn this one properly and the other six structure diagrams cost almost nothing.
11 min readUML 2.5.12 of 15
01Reading the box
A class is a rectangle with up to three compartments, stacked. Only the first is required.
- Name. The type name, centred and bold. If the class is abstract, the name is italic. A keyword in guillemets above it -
«interface»,«enumeration»- narrows what kind of classifier it is. - Attributes. One per line, written
visibility name: Type [multiplicity] = default. Everything but the name is optional. - Operations. One per line, written
visibility name(parameters): ReturnType.
The leading symbol on each member is its visibility:+ public, - private, # protected, and ~ package. A underlined member is static - it belongs to the class rather than to an instance.
02The six lines
Almost all of the meaning in a class diagram is in the lines, and there are only six worth memorising. The end that carries the decoration is significant in every case.
| Element | Notation | What it means |
|---|---|---|
| Association | A structural link. Instances of one know about instances of the other. Multiplicity at each end says how many. | |
| Directed association | The same, but navigable only in the arrow's direction. Payment can reach its PaymentMethod; the method cannot reach back. | |
| Aggregation | A whole-part link where the part survives the whole. Hollow diamond sits at the whole. | |
| Composition | A whole-part link where the part dies with the whole. Filled diamond at the whole. A part has exactly one owner. | |
| Generalization | Inheritance. Hollow triangle points at the parent. Read it as "is a kind of". | |
| Realization | Implementation of an interface. Dashed line, hollow triangle at the interface. | |
| Dependency | The weakest link: one uses the other, typically as a parameter or a local. Use it sparingly or every class ends up connected to every other. |
Solid triangle-free lines are structural; dashed lines are always weaker than solid ones.
03Multiplicity
The number at the end of a line says how many instances participate. It sits at the far end from the class it constrains, which trips people up: reading Merchant 1 —— 0..* Payment, the 0..* next to Payment means one merchant has zero or more payments.
1- exactly one. The default when nothing is written, though writing it is clearer.0..1- optional. This is the notation for a nullable reference.*or0..*- any number, including none.1..*- at least one. A meaningful and frequently-forgotten constraint.2..4- an explicit range.
Multiplicity is the cheapest correctness win in the whole notation. "Can this be empty?" and "can there be more than one?" are the two questions that surface real disagreements in a design review, and the answer takes three characters to record.
04Association, aggregation, composition
These three are the same shape of relationship at three strengths, and the difference is about lifecycle, not about how strongly the two things feel related.
A plain association claims nothing about ownership. Two things are linked. That is all, and it is the right default.
Aggregation - the hollow diamond - claims a whole-part relationship where the part is independent. Honest advice: aggregation carries almost no formal semantics in UML 2.5.1, and readers disagree about what it implies. If you are unsure between association and aggregation, draw the association.
Composition - the filled diamond - is the one that says something strong and checkable. The part belongs to exactly one whole and is destroyed with it. In the payments model, a Refund exists only as part of a Payment; there is no free-floating refund. That is a real constraint, worth recording, and it maps directly to a cascade delete or an owned collection in code.
05When to draw one
Reach for it when
- Agreeing the domain vocabulary with people who are not going to read the code
- The relationships have real constraints - optionality, cardinality, ownership
- Onboarding somebody to a subsystem whose shape is not obvious from the file tree
- Designing a schema, an API contract, or anything with a persisted structure
Reach for something else when
- You would be redrawing what an IDE generates from the source in one click
- The question is about order or timing - draw a sequence or activity diagram
- The classes are pure framework plumbing with no domain meaning
- You are tempted to include every class in the system on one canvas
The most useful class diagrams in practice are small. Seven to twelve classes, attributes on the ones being discussed and omitted everywhere else, and every line carrying a multiplicity. A diagram like that gets read in a meeting and settles arguments. A hundred-box class diagram is an artefact, not a communication.
06Five mistakes worth avoiding
- Arrows pointing the wrong way. Generalization and realization point at the abstraction. If your triangle is on the subclass, the diagram says the opposite of what you meant.
- Composition used for "strongly related". The filled diamond is a lifecycle claim. Use it only when destroying the whole really does destroy the part.
- No multiplicities. A line with nothing at either end has thrown away the most valuable information the diagram could carry.
- Mixing altitudes. Domain classes and framework classes on one canvas. Split it; each diagram should be readable by one audience.
- Modelling every getter. Operations belong on the diagram when they carry meaning.
getName()does not.
If you want to check a class model against reality, draw one object diagram - a single concrete snapshot of instances. Multiplicities that looked fine in the abstract tend to fall over immediately when you try to fill them in with real values.
In one line each
- 01Three compartments: name, attributes, operations. Omit rather than empty.
- 02Visibility markers are + - # ~; underline means static, italic name means abstract.
- 03Six lines carry the meaning; the decorated end is always significant.
- 04Generalization and realization point at the abstraction.
- 05Composition is a lifecycle claim - the part dies with the whole. Aggregation barely means anything; prefer a plain association.
- 06Multiplicity is the cheapest correctness win available. Put it on every line.
Related reading