merIDK / Diagram types / Class diagram
Mermaid class diagram: example and syntax
Class diagrams describe the structure of object-oriented code: classes, their fields and methods, and how classes relate through inheritance, composition and association.
Open this example in the editor
Example code
classDiagram
direction LR
class Animal {
<<abstract>>
+String name
+int age
+makeSound()* void
}
class Dog {
+fetch() bool
}
class Owner {
-List~Animal~ pets
+adopt(Animal a)
}
Animal <|-- Dog
Animal <|-- Cat
Owner "1" o-- "*" Animal : owns
Result
classDiagram
direction LR
class Animal {
<<abstract>>
+String name
+int age
+makeSound()* void
}
class Dog {
+fetch() bool
}
class Owner {
-List~Animal~ pets
+adopt(Animal a)
}
Animal <|-- Dog
Animal <|-- Cat
Owner "1" o-- "*" Animal : ownsSyntax quick reference
- Visibility:
+public,-private,#protected,~package. - Relations:
<|--inheritance,*--composition,o--aggregation,-->association. - Generics use tildes:
List~Animal~. - Annotations:
<<interface>>,<<abstract>>.
How to use it
- Open the example in the editor, or paste your own Mermaid code.
- Edit the code on the left. The preview updates as you type, and errors point to the line.
- Export SVG or PNG, copy a Markdown block for GitHub or Notion, or copy a share link.