Skip to content

UML Class Diagram

A class diagram shows classes and the links between them. See UML Class Diagram Relationship for the full set of relationship types.

Associations. An association is a line between two classes. It usually stands for something like a field in a class. If class A holds a reference to class B, you draw an association from A to B.

Dependencies. A dependency means that a change to one element (the supplier) may force a change in another (the client). Associations and generalizations already imply a dependency. So there is no need to draw an extra dependency line next to them.

Interfaces. UML 2 added the socket notation to show interfaces a class requires. It grew out of the lollipop notation, which shows interfaces a class provides. The UML committee ruled that the combined ball-and-socket notation may only be used for connectors in Composite Structure Diagrams. It does not link interfaces in class diagrams. See Provided and Required Interface for more.

Stay selective. Use class diagrams as sketches. Show only the classes that are interesting and worth discussing before you read the code. The goal is communication, not a complete specification. Showing every class makes the diagram harder to understand.

A team sketches an online shop. The diagram shows three classes: Order, Customer and Product. An association from Order to Customer reflects that an order holds a reference to its customer. Helper classes stay out of the picture. The team uses the sketch to agree on the core ideas before looking at code.

A selective sketch is quick to read and good for discussion. It leaves classes out, so it is not a complete specification. Drawing every class would be more complete, but it would hurt comprehensibility.

A selective sketch: three classes, with associations that read like fields.

Diagram source
@startuml
class Order
class Customer
class Product
Order --> Customer : placed by
Order --> "1..*" Product
@enduml
  • The class diagram shows only the interesting classes, not every class in the system.
  • Dependency lines are not drawn in addition to associations or generalizations between the same classes.
  • The ball-and-socket notation is not used to link interfaces in class diagrams.