How to Write UML Class Diagram: Simple Guide and Example
To write a
UML class diagram, start by defining classes with their name, attributes, and methods inside boxes. Connect classes using lines to show relationships like inheritance, association, or dependency. Use standard UML symbols and labels to clearly represent your system's structure.Syntax
A UML class diagram uses boxes to represent classes. Each box has three parts: the class name at the top, attributes in the middle, and methods at the bottom. Relationships between classes are shown with lines and arrows:
- Inheritance: solid line with a hollow arrow pointing to the parent class.
- Association: solid line connecting classes, optionally with multiplicity (e.g., 1..*).
- Dependency: dashed line with an open arrow.
Visibility symbols before attributes and methods indicate access: + for public, - for private, and # for protected.
plaintext
ClassName
+ attribute1: Type
- attribute2: Type
+ method1(param: Type): ReturnType
- method2(): voidExample
This example shows two classes, Person and Student, where Student inherits from Person. It includes attributes with visibility and methods with parameters.
plaintext
Person
+ name: String
+ age: int
+ getDetails(): String
Student
+ studentId: String
+ getDetails(): String
Student --|> PersonOutput
A UML class diagram with two boxes labeled Person and Student. Person has public attributes name and age, and a method getDetails(). Student inherits from Person and adds studentId and overrides getDetails().
Common Pitfalls
Common mistakes include:
- Not separating class name, attributes, and methods clearly.
- Using incorrect symbols for visibility or relationships.
- Omitting multiplicity on associations, which causes confusion about how many objects relate.
- Mixing dependency and association lines.
Always keep your diagram clean and consistent for easy understanding.
plaintext
Wrong: Person name: String age: int getDetails() Student studentId: String getDetails() Student --> Person Right: Person + name: String + age: int + getDetails(): String Student + studentId: String + getDetails(): String Student --|> Person
Quick Reference
| Element | Symbol/Notation | Meaning |
|---|---|---|
| Class Name | Top box text | Name of the class |
| Attributes | +/-/# name: Type | Class properties with visibility |
| Methods | +/-/# name(params): ReturnType | Class functions with visibility |
| Inheritance | Solid line with hollow arrow | Child class inherits from parent |
| Association | Solid line | Classes connected with relationship |
| Dependency | Dashed line with open arrow | One class depends on another |
| Visibility | + public, - private, # protected | Access level of members |
Key Takeaways
Use boxes divided into name, attributes, and methods to represent classes clearly.
Show relationships with correct UML lines and arrows for inheritance, association, and dependency.
Mark visibility of attributes and methods with +, -, or # symbols.
Keep multiplicity on associations to clarify object counts.
Avoid mixing symbols and keep diagrams clean for easy understanding.