UML class diagram relationships, examples
The three-part class box and the six UML relationships, from inheritance to dependency, with editable class diagram examples.
What is a class diagram
A class diagram is a UML picture of the classes in a program, their attributes and methods, and how classes relate. Use it to plan structure before coding or to explain existing code.
- Object-oriented programming assignments and design documents
- UML relationship questions in exams
- Explaining the structure of a library or service
Symbols and meanings
| Symbol | Meaning |
|---|---|
| Class | A box with name, attributes and methods. |
| Interface | Marked «interface»; methods only. |
| Inheritance | Solid line from child to parent, hollow triangle at the parent. |
| Realization | Dashed line to the interface, hollow triangle. |
| Composition | Filled diamond at the whole; parts die with it. |
| Aggregation | Hollow diamond at the whole; parts can live alone. |
| Association | One class knows and uses another; solid arrow. |
| Dependency | Uses another briefly (a parameter); dashed arrow. |
Basis: UML 2.5 클래스 다이어그램(OMG), 객체지향 설계 수업 범위
How to draw one
- Split the box into name, attributes and methods.
- Mark visibility with + (public), - (private), # (protected).
- "X is a kind of Y" is inheritance: solid line, hollow triangle.
- Implementing an interface is realization: dashed line, hollow triangle.
- Parts that live and die with the whole are composition (filled diamond); parts that can exist alone are aggregation (hollow diamond).
Mermaid classDiagram: class Member { +name String / +login() bool }, Parent <|-- Child, Interface <|.. Impl, Whole *-- Part, Whole o-- Part, A --> B, A ..> B.
Common mistakes
- The inheritance arrow starts at the parent (the triangle belongs at the parent).
- Composition or aggregation diamonds on the part end (they belong on the whole).
- An interface drawn with a solid inheritance line.
- Inheritance that loops back to itself.
The editor's notation check flags these mistakes every time the picture changes.
12 class diagram examples
- Shape inheritance class diagram
- Payment interface class diagram
- Car composition and aggregation class diagram
- Library system class diagram
- Animal polymorphism class diagram
- Bank account class diagram
- MVC structure class diagram
- Shopping cart class diagram
- Observer pattern class diagram
- School class roster class diagram
- Repository pattern class diagram
- Game character class diagram