What is a Class Diagram?
A Class Diagram is the most commonly used UML diagram. It shows the classes in a system, their attributes, operations, and the relationships between them. It's a static structural diagram β it captures the architecture of the system at a point in time, not how it behaves over time.
Class diagrams serve as blueprints for developers. They document the system's structure and guide implementation. When someone reads your class diagram, they should understand the system's key entities and how they connect.
Class Notation
A class is represented as a rectangle divided into three compartments:
ββββββββββββββββββββββββββββββββββββ
β ClassName β β Name compartment
ββββββββββββββββββββββββββββββββββββ€
β - attribute: Type β β Attribute compartment
β - anotherAttribute: Type β
β + computedValue: Type β
ββββββββββββββββββββββββββββββββββββ€
β + operation(param): ReturnType β β Operation compartment
β - helperMethod(): void β
β # protectedMethod(): void β
ββββββββββββββββββββββββββββββββββββ
Visibility Symbols:
+ public β accessible from anywhere
- private β accessible only within the class
# protected β accessible within the class and subclasses
~ package β accessible within the same package
Example: Bank Account Class
ββββββββββββββββββββββββββββββββββββ
β BankAccount β
ββββββββββββββββββββββββββββββββββββ€
β - accountId: String β
β - owner: String β
β - balance: double β
β - accountType: AccountType β
ββββββββββββββββββββββββββββββββββββ€
β + deposit(amount: double): void β
β + withdraw(amount: double): void β
β + getBalance(): double β
β + getStatement(): List<Transaction>β
β - validateAmount(amount): void β
β - updateBalance(amount): void β
ββββββββββββββββββββββββββββββββββββ
Relationship Types
Class diagrams show several types of relationships between classes:
1. Association (solid line)
ββββββββββββββββββββββββ
A general relationship. One class "knows about" another.
Customer ββββββββββββββ Order
(Customer places Orders)
2. Aggregation (hollow diamond)
βββββββββββββββββββββββββββββ
"Has-a" relationship where the part can exist independently.
Department βββββββββββ Teacher
(Department has Teachers)
3. Composition (filled diamond)
βββββββββββββββββββββββββββββ
"Has-a" relationship where the part cannot exist without the whole.
Order βββββββββββ OrderItem
(Order owns OrderItems)
4. Inheritance (hollow triangle arrow)
ββββββββββββββββββββββββββββββββββββ
"Is-a" relationship β subclass inherits from superclass.
SavingsAccount βββββββ· BankAccount
(SavingsAccount is a BankAccount)
5. Realization (dashed line with hollow triangle)
ββββββββββββββββββββββββββββββββββββββββββββββ
A class implements an interface.
- - - - - - - - - - - - β· Payable
(Payment implements Payable)
6. Dependency (dashed arrow)
βββββββββββββββββββββββββ
A temporary relationship β one class uses another.
OrderHandler - - - - - β· EmailService
(OrderHandler uses EmailService temporarily)
Multiplicity
Multiplicity indicates how many instances of one class can be associated with instances of another class. It's written as a number or range near the relationship line.
Multiplicity Notation:
1 β exactly one
0..1 β zero or one (optional)
* β zero or more (many)
1..* β one or more
0..* β zero or more
2..5 β between 2 and 5
Examples:
ββββββββββββββββ 1 ββββββββββββββββ
β Customer ββββββββββββββββββΆβ Account β
β β β β
ββββββββββββββββ ββββββββββββββββ
Customer has exactly 1 Account
ββββββββββββββββ 1..* ββββββββββββββββ
β Customer ββββββββββββββββββΆβ Order β
β β β β
ββββββββββββββββ ββββββββββββββββ
Customer has one or more Orders
ββββββββββββββββ 0..* ββββββββββββββββ
β Order ββββββββββββββββββΆβ OrderItem β
β β β β
ββββββββββββββββ ββββββββββββββββ
Order has zero or more OrderItems
Complete Class Diagram Example
Library Management System:
ββββββββββββββββββββ ββββββββββββββββββββ
β Library β* 1β Book β
ββββββββββββββββββββ€ββββββββββββββββββββββββββββββββββ€
β - name: String β β - title: String β
β - address: Stringβ β - author: String β
ββββββββββββββββββββ€ β - isbn: String β
β + getCatalog() β β - isAvailable: β
β : List<Book> β β boolean β
β + addBook(book) β ββββββββββββββββββββ€
ββββββββββββββββββββ β + checkOut(): β
β boolean β
1 β + returnBook(): β
β β void β
β 1..* β + reserve(): β
ββββββββ΄βββββββββββ β void β
β Member β ββββββββββββββββββββ
βββββββββββββββββββ€
β - memberId: Str β 1 ββββββββββββββββββββ
β - name: String ββββββββββββββββ Loan β
β - email: String β 0..* ββββββββββββββββββββ€
βββββββββββββββββββ€ β - loanDate: Date β
β + borrowBook() β β - dueDate: Date β
β + returnBook() β β - returnDate:Dateβ
β + getLoans() β ββββββββββββββββββββ€
βββββββββββββββββββ β + isOverdue(): β
β boolean β
β + calculateFine()β
β : double β
ββββββββββββββββββββ
Best Practices
Follow these guidelines when creating class diagrams:
DO:
β Show only classes relevant to the current design level
β Use meaningful names that match the domain
β Include multiplicities on associations
β Group related classes together
β Show key attributes and operations (not every field)
β Use inheritance to show "is-a" relationships
DON'T:
β Create diagrams with too many classes (aim for 10-15 max)
β Show every private attribute and method
β Use implementation-specific types (ArrayList instead of List)
β Mix design-level and code-level details
β Ignore visibility modifiers (+, -, #)