Class Diagram
Source: PlantUML: Class Diagram Digest Date: January 15, 2023
Note: The UML preview of this documentation is powered by mermaid, some PlantUML symbols are not supported by mermaid(e.g. #--
), so I didn't record these special symbols.
1. Declaring Element

Note: protocol
and struct
was added in PR-1028.
2. Relations between Classes
Relations between classes are defined using the following symbols :
Extension
`<
--`
Composition
*--
Aggregation
o--
It is possible to replace --
by ..
to have a dotted line.
Knowing those rules, it is possible to draw the following drawings:
Source:
@startuml
Class01 <|-- Class02
Class03 *-- Class04
Class05 o-- Class06
Class07 .. Class08
Class09 -- Class10
@enduml
Preview by using mermaid:
classDiagram
Class01 <|-- Class02
Class03 *-- Class04
Class05 o-- Class06
Class07 .. Class08
Class09 -- Class10
Source:
@startuml
Class11 <|.. Class12
Class13 --> Class14
Class15 ..> Class16
Class17 ..|> Class18
Class19 <--* Class20
@enduml
Preview by using mermaid:
classDiagram
Class11 <|.. Class12
Class13 --> Class14
Class15 ..> Class16
Class17 ..|> Class18
Class19 <--* Class20
3. Label on Relations
It is possible to add a label on the relation, using :
, followed by the text of the label.
For cardinality, you can use double-quotes ""
on each side of the relation.
Source:
@startuml
Class01 "1" *-- "many" Class02 : contains
Class03 o-- Class04 : aggregation
Class05 --> "1" Class06
@enduml
Preview by using mermaid:
classDiagram
Class01 "1" *-- "n" Class02 : contains
Class03 o-- Class04 : aggregation
Class05 --> "1" Class06
You can add an extra arrow pointing at one object showing which object acts on the other object, using <
or >
at the begin or at the end of the label.
Source:
@startuml
class Car
Driver - Car : drives >
Car *- Wheel : have 4 >
Car -- Person : < owns
@enduml
Preview by using mermaid:
classDiagram
class Car
Driver -- Car : drives >
Car *-- Wheel : have 4 >
Car -- Person : < owns
Warning: This feature is not fully supported by mermaid.
4. Using non-letters in element names and relation labels
Seems not so useful, omit for now.
5. Adding methods
To declare fields and methods, you can use the symbol :
followed by the field's or method's name.
The system checks for parenthesis to choose between methods and fields.
Source:
@startuml
Object <|-- ArrayList
Object : equals()
ArrayList : Object[] elementData
ArrayList : size()
@enduml
Preview by using mermaid:
classDiagram
Object <|-- ArrayList
Object : equals()
ArrayList : Object[] elementData
ArrayList : size()
It is also possible to group between brackets {}
all fields and methods.
Note that the syntax is highly flexible about type/name order.
Source:
@startuml
class Dummy {
String data
void methods()
}
class Flight {
flightNumber : Integer
departureTime : Date
}
@enduml
Preview by using mermaid:
classDiagram
class Dummy {
String data
void methods()
}
class Flight {
flightNumber : Integer
departureTime : Date
}
You can use {field}
and {method}
modifiers to override default behaviour of the parser about fields and methods.
Source:
@startuml
class Dummy {
{field} A field (despite parentheses)
{method} Some method
}
@enduml
Preview by using mermaid:
classDiagram
class Dummy {
{field} A field (despite parentheses)
{method} Some method
}
Warning: This feature is not supported by mermaid.
6. Defining visibility
Last updated
Was this helpful?