Class Diagram: A Complete Guide

Class Diagram is widely used in object-oriented programming to visualize and design the structure of software systems before writing any code.

What is a Class Diagram?

A Class Diagram is a type of static structure diagram in the Unified Modeling Language (UML) that describes the structure of a system by showing its classes, attributes, methods, and the relationships among objects.

Key Components of a Class Diagram

Class

Represented by a rectangle divided into three parts:

  • Top: Class name
  • Middle: Attributes (variables/properties)
  • Bottom: Methods (functions/operations)
-------------------
|   Person         |
-------------------
| - name: String   |
| - age: int       |
-------------------
| + speak(): void  |
| + walk(): void   |
-------------------
Python

Attributes

  • Represent class properties or fields.
  • Format: visibility name: type
  • Example: – name: String

Methods

  • Represent operations the class can perform.
  • Format: visibility name(parameter: type): returnType
  • Example: + walk(): void

Visibility

  • + Public
  • – Private
  • # Protected

Relationships

  • Association (→) – “has a” relationship
  • Inheritance/Generalization (⇨) – “is a” relationship
  • Aggregation (◇) – whole-part with independent lifecycle
  • Composition (◆) – whole-part with dependent lifecycle
  • Dependency (—>) – one class uses another

Types of Relationships in Detail

Class Relationships

Association: Shows that one class is connected to another.

  • A basic connection between classes (bidirectional or unidirectional).
  • Example: A Person has an Address.
  • The arrow (if shown) usually points from the “user” class to the “used” class.
Person → Address
Python

Inheritance (Generalization): Indicates one class is a subclass of another.

  • Example: A Dog is a Animal.
  • Arrow points to the parent (superclass).
Animal ⇨ Dog
Python

Aggregation: A weak “whole-part” relationship.

  • Example: A Team has Players.
  • Diamond is on the “whole” side (Team), and arrow points to the “part” (Player).
Team ◇────→ Player
Python

Composition: A strong “whole-part” relationship.

  • Example: A House contains Rooms.
  • Filled diamond on the whole (House), pointing to the part (Room).
House ◆────→ Room
Python

Dependency: Represents a temporary relationship (e.g., method parameters).

  • Example: Order uses Payment.
  • Dashed arrow pointing to the dependent class (Payment).
Order ────→ Payment
Python

Summary

RelationshipSymbol/NotationArrow DirectionMeaning
Association─────→From user to used“has a”
Inheritance─────▶From child to parent“is a”
Aggregation◇────→Diamond at wholeWhole-part (independent life)
Composition◆────→Filled diamond at wholeWhole-part (dependent life)
Dependency─ ─ ─→From client to supplier“uses” temporarily

Conclusion

Class diagrams are essential in software design—they help model the static structure of systems, understand object relationships, and communicate design clearly to stakeholders. Whether you’re designing a small module or a complex system, class diagrams give you a bird’s-eye view of your application’s architecture.

Leave a Comment