Labs ICT
⭐ Pro Login

Class Diagrams in Detail

Detailed class diagrams showing attributes, operations, and relationships.

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 (+, -, #)

πŸ§ͺ Quick Quiz

What does a class diagram show?