All diagram types Open editor

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 : owns

Syntax quick reference

How to use it

  1. Open the example in the editor, or paste your own Mermaid code.
  2. Edit the code on the left. The preview updates as you type, and errors point to the line.
  3. Export SVG or PNG, copy a Markdown block for GitHub or Notion, or copy a share link.

More diagram types