Reading a class box
Each class is a box with three parts:
- Name at the top, for example
Order. - Attributes (data) in the middle, such as
+int id. - Methods (actions) at the bottom, such as
+total() float.
The sign in front says who can use it:
| Sign | Visibility |
|---|---|
+ | Public: anyone |
- | Private: only this class |
# | Protected: this class and its children |
~ | Package: the same module |
The lines between classes
| Relationship | Drawn as | Means | Example |
|---|---|---|---|
| Inheritance | Arrow with a hollow triangle | "is a" | Dog is an Animal |
| Association | Plain line or arrow | "uses" or "knows" | Owner has Dogs |
| Aggregation | Line with a hollow diamond | "has", parts can live alone | Team has Players |
| Composition | Line with a filled diamond | "owns", parts die with it | House has Rooms |
| Dependency | Dashed arrow | "depends on" for a moment | Report uses Printer |
Numbers at the ends, called multiplicity, say how many: 1, 0..1, * (many), 1..* (one or more).
Example: pets and owners
The text behind it:
classDiagram
class Animal {
+String name
+int age
+makeSound() void
}
class Dog {
+fetch() void
}
Animal <|-- Dog
Owner "1" --> "*" Animal : owns
Relationship arrows in text
A <|-- B B inherits from A
A *-- B A is composed of B
A o-- B A aggregates B
A --> B A is associated with B
A ..> B A depends on B
A "1" --> "*" B : label with multiplicity and a label
From code to a class diagram
If the classes already exist in code, you do not have to type them. Chalkline reads Python, Java, TypeScript, C++ and Go and draws a class diagram of the file: classes, fields, methods and inheritance. Open Diagram as text → From code, paste the file and choose Classes.
Tips for clear class diagrams
- Show only the classes that matter for the point you are making.
- Leave out getters, setters and obvious fields.
- Put parent classes above their children so inheritance arrows point up.
- One diagram per feature or module is easier to read than one for the whole system.
Draw yours for free in Chalkline, then export to SVG, PDF, PowerPoint or TikZ for a report. For database tables instead of classes, see SQL to ER diagram.