0
0
LldHow-ToBeginner ยท 4 min read

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(): void
๐Ÿ’ป

Example

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 --|> Person
Output
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

ElementSymbol/NotationMeaning
Class NameTop box textName of the class
Attributes+/-/# name: TypeClass properties with visibility
Methods+/-/# name(params): ReturnTypeClass functions with visibility
InheritanceSolid line with hollow arrowChild class inherits from parent
AssociationSolid lineClasses connected with relationship
DependencyDashed line with open arrowOne class depends on another
Visibility+ public, - private, # protectedAccess 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.