Learn
Class diagram syntax
Learn class blocks, visibility, relationships, and stereotypes used in the Editor modules template.
On this page9 sections
Syntax cheatsheet
Quick reference for this diagram type
| Concept | Syntax |
|---|---|
| Declare | classDiagram |
| Class body | class Name { … } |
| Visibility | + public · - private · # protected · ~ package |
| Association | Editor --> Preview |
| Inheritance | Child <|-- Parent |
| Composition / aggregation | *-- · o-- |
| Stereotype | <<interface>> |
- 1
Start a class diagram
Begin with classDiagram. Define classes first, then connect them with relationship arrows.
Class names are identifiers — keep them aligned with real module names when documenting code.
classDiagram class Editor class Preview Editor --> Preview
Expand - 2
Fields and methods
List members inside the class body. Fields are names (optionally typed); methods end with ().
Order fields above methods so the box reads like a quick API card.
classDiagram class Editor { +content +save() } class Preview { +render() }Expand - 3
Visibility markers
Prefix members with + public, - private, # protected, or ~ package/internal.
In product diagrams, + is often enough; use - when you want to hide implementation details.
classDiagram class Arrange { +layout -cache +exportPng() }Expand - 4
Relationships between classes
--> is a directed association (“uses”). <|-- is inheritance. *-- composition and o-- aggregation show ownership strength.
Label relationships when the verb isn’t obvious: Editor --> Preview : renders.
classDiagram Editor --> Preview : renders Editor --> Arrange : arranges Shape <|-- Rectangle Document *-- Page
Expand - 5
Stereotypes and annotations
Stereotypes like <<interface>> or <<service>> clarify the role of a class without extra nodes.
Place the stereotype in the class declaration or as a line in the body, depending on style.
- Tip: Dashed realization (<|..) often marks interface implementation.
classDiagram class Renderer { <<interface>> +render() } class MermaidPreview { +render() } Renderer <|.. MermaidPreviewExpand - 6
Cardinality on associations
You can attach multiplicity on either end of an association when it helps ("1", "0..*").
Skip multiplicity when every relationship is obviously one-to-one — noise helps no one.
classDiagram Editor "1" --> "*" Preview : may open
Expand - 7
Put it together: Editor modules
Combine class bodies and associations to show how Editor drives Preview and Arrange in the examples template.
classDiagram class Editor { +content +save() } class Preview { +render() } class Arrange { +layout +exportPng() } Editor --> Preview Editor --> ArrangeExpand
Check your level
80% or higher certifies this lesson. You'll get 3–5 random questions from a larger bank.