Class Diagram Online Editor
Draw UML class diagrams with Mermaid online: attributes, methods, visibility, inheritance, composition and interfaces, with live preview and free image export.
Try it
Edit the code; the preview updates as you type.
Rendering diagram…
What is a class diagram?
A UML class diagram shows the classes in a system, the data and methods each one has, and how they relate through inheritance, composition, aggregation and association.
In Mermaid every class diagram starts with classDiagram. Members are listed inside braces with a visibility prefix, and relationships are short arrows between class names.
When to use it
- Designing an object model before you write the code
- Documenting interfaces and their implementations
- Explaining a design pattern such as strategy or observer
- Onboarding developers to an unfamiliar codebase
- UML coursework and exam preparation
Mermaid class diagram syntax
Every diagram of this type starts with classDiagram.
| Syntax | Meaning |
|---|---|
class Name { } | Declares a class; members go inside the braces, one per line. |
+ - # ~ | Visibility: public, private, protected and package or internal. |
+method(arg) ReturnType | A method; the return type follows the closing parenthesis. |
A <|-- B | Inheritance: B extends A. |
A <|.. B | Realization: B implements interface A. |
A *-- B | Composition: B cannot exist without A. |
A o-- B | Aggregation: A holds B, but B can exist on its own. |
A --> B / A ..> B | Association and dependency. |
A "1" --> "*" B | Cardinality labels on either end of a relationship. |
List~T~ | Generic types use tildes instead of angle brackets. |
<<interface>> | An annotation such as interface, abstract or enumeration. |
More examples
Inheritance with an abstract base class
classDiagram
class Shape {
<<abstract>>
+area()* double
+count$ int
}
Shape <|-- Circle
Shape <|-- Rectangle
class Circle {
+double radius
}
class Rectangle {
+double width
+double height
}Common mistakes and how to fix them
- Generics are written with angle brackets, such as List<String>.
- Use tildes: List~String~.
- A class name contains spaces or a dot.
- Use a single identifier, or give it a label: class Order["Customer order"].
- The arrow points the wrong way for inheritance.
- The triangle sits next to the parent: Parent <|-- Child.
Export your class diagram
Download a PNG, SVG or PDF straight from the playground above. For more options, such as a 1x to 4x scale, a transparent background or copying the image, choose Open in editor and use the Export menu there. Export is free, needs no account and adds no watermark.
Frequently asked questions
How do I show inheritance in a Mermaid class diagram?
Write Parent <|-- Child. The hollow triangle is drawn next to the parent class. Use <|.. when a class implements an interface.
What is the difference between composition and aggregation?
Composition (*--) means the part cannot exist without the whole, like an order and its line items. Aggregation (o--) means the part can exist on its own, like a team and its players.
Can I mark a class as an interface or abstract class?
Yes. Put an annotation such as <<interface>> or <<abstract>> on the first line inside the class block.
Can I export the class diagram for a report?
Yes. Export a high-resolution PNG for documents and slides, an SVG for lossless scaling, or a PDF for printing. All exports are free.