Skip to content
DiaGO

Class Diagrams

The workhorse of UML - classes, their members, and the relationships between them. Read this to decode or draw any class diagram.

Open this example in Studio

A class diagram shows the static structure of a design: which classes exist, what they know (attributes), what they can do (operations), and how they relate to one another. It is by far the most common UML diagram — most "UML" you meet in the wild is a class diagram — so reading one precisely pays off immediately. If you need a refresher on what classes and objects are in the first place, start with Objects and Classes.

The class box

A class is drawn as a rectangle with up to three compartments, stacked top to bottom:

  1. Name — required. Bold and centered by convention.
  2. Attributes — the data the class holds.
  3. Operations — the behavior it offers.
+----------------------------------+
| Book                             |
+----------------------------------+
| - isbn: String                   |
| - title: String                  |
| # status: BookStatus             |
+----------------------------------+
| + checkOut(member: Member): Loan |
| + isAvailable(): Boolean         |
+----------------------------------+

An empty or omitted compartment is not a claim that the class has no members — it means the author chose not to show them. This elision is deliberate and useful: a diagram is an argument about what matters in the current discussion, not a dump of the source code. Showing only the relevant members is normal practice, not laziness.

Member syntax

UML defines a formal grammar for members. The two forms you need:

attribute:  visibility name: Type
operation:  visibility name(param: Type): ReturnType

The type comes after a colon, TypeScript-style, not before the name as in Java. The box above corresponds to:

class Book {
  private isbn: string;
  private title: string;
  protected status: BookStatus;
 
  public checkOut(member: Member): Loan {
    /* ... */
  }
  public isAvailable(): boolean {
    /* ... */
  }
}

Collection-valued attributes can carry a multiplicity, e.g. loans: Loan [0..*], instead of committing to a concrete type like List<Loan>. The diagram states intent — many loans — and lets the code choose the data structure.

Naming

The UML specification's style guidelines capitalize class names and start attribute and operation names with a lower-case letter, and the mainstream object-oriented languages agree. So classes and interfaces are written in UpperCamelCase (OrderLine, PaymentGateway), and attributes and operations in lowerCamelCase (dueDate, isAvailable()). Two exceptions are common: a static constant may be written MAX_LOANS, and a constructor carries its class's name. A name like order line or Get_Items is not wrong UML, but it reads like a note rather than a type or member, and it does not map straight to code.

Visibility

SymbolVisibilityWho can access
+publicany class
-privatethe declaring class only
#protectedthe class and its subclasses
~packageclasses in the same package or module

Visibility markers are where a class diagram encodes encapsulation. A diagram where everything is + is telling you the design has no boundary story yet. Deciding which class should own a piece of state — and therefore keep it private — is exactly the question Information Expert helps you answer.

Abstract classes, interfaces, and static members

  • Abstract classes have their name written in italics. This is formal UML notation — but italics do not survive a whiteboard, so hand-drawn diagrams often add a textual annotation next to the name instead. An individual operation shown in italics is likewise abstract.
  • Interfaces carry the «interface» stereotype keyword above the name and are conventionally shown with only an operations compartment: they list what implementers must provide.
  • Static members are shown underlined. This is standard UML notation, not just a habit: it marks class-level features apart from instance-level ones.
  • Enumerations carry the «enumeration» keyword above the name and list their literals — the only values the type can take — one per line in a compartment of their own (ACTIVE, RETURNED, OVERDUE). An operations compartment appears only if the enumeration has operations. Use the enumeration's name as an attribute's type (status: LoanStatus) rather than drawing a line to it: an enumeration is a value type, not an object others hold references to.

Relationships at a glance

Classes rarely stand alone. UML gives you a small vocabulary of connections, each with its own article:

  • Association — a solid line: one class holds a durable structural link to another, as a Loan refers to its Book. See Associations.
  • Aggregation — a hollow diamond on the whole: a whole-part relationship where parts can exist independently of the whole. See Aggregation.
  • Composition — a filled diamond on the whole: the whole owns its parts, and the parts cannot outlive it, as an Order owns its OrderLines. See Composition.
  • Generalization — a solid line with a hollow triangle pointing at the parent: an is-a relationship, as EBook is a Book. See Generalization.
  • Realization — a dashed line with a hollow triangle pointing at the interface: a class implements the contract an interface declares, as EmailNotifier realizes Notifier. See Generalization.
  • Dependency — a dashed arrow: the weakest link; one class uses another transiently, for example as a parameter type or local variable. See Dependency.

What class diagrams are not for

A class diagram is a structural snapshot. It says nothing about:

  • Control flow. The operations compartment tells you checkOut exists — not when it is called, in what order, or what happens when it fails. That is the job of sequence and activity diagrams.
  • Timing and concurrency. Nothing in the notation expresses before, after, or at-the-same-time.
  • Persistence. Class boxes look like database tables, but they model behavior-carrying objects. If your boxes contain only attributes and every line is a plain association, you may actually be drawing an ER model — a related but distinct tool.

Reading control flow into a class diagram is the most common misreading. Resist it.

Scope: one diagram, one story

The most frequent failure mode is not wrong notation but wrong scope: a single auto-generated picture containing every class in the codebase. That diagram is technically accurate and communicates nothing.

A useful heuristic — advice, not a rule: keep a diagram to roughly five to nine classes, all serving one narrative, such as how checkout works or what the payment type hierarchy looks like. Draw several small diagrams rather than one large one, and elide members aggressively in each. The same class may appear in many diagrams, showing different members each time.

Reach for it

  • Explaining the structure of one subsystem to a new teammate
  • Deciding which class should own a given piece of state or behavior
  • Documenting the classes that participate in a design pattern

Avoid it

  • Showing the order in which operations are called
  • Capturing every class in the system in a single picture
  • Modeling database tables where an ER diagram fits better

Practice

Model a small lending library, then compare against the library-system example in Studio. Draw a class diagram with Library, Member, Book, and Loan:

  1. Give each class two or three attributes and one or two operations, using full UML member syntax with visibility markers.
  2. Use at least three different relationship kinds, with one written sentence justifying each — in particular, defend whether the Library-to-Book link is aggregation or composition.
  3. Mark one class or operation abstract, or extract an «interface», and state what that abstraction buys you.

When you are done, hand the diagram to someone who has not seen any code and ask them to say what it claims. If they cannot, the diagram — not the reader — needs work.