What is a UML Class Diagram?
A UML (Unified Modeling Language) class diagram is a visual representation of classes and their relationships in an object-oriented system. It helps in understanding the structure of the system and shows how classes interact with each other. A class diagram typically shows:- Class name
- Attributes (properties)
- Methods (functions)
- Relationships (associations, inheritance, composition, etc.)
Class Notation
Each class is represented as a box divided into three sections: name, attributes, and methods.+Public-Private#Protected
Relationship Types
Association
Association
Association represents a bi-directional relationship between two classes where one uses or interacts with another. It means uses, has-a, works with, or belongs to. One class holds a permanent reference to another as part of its state.Example: A
Car has an Engine and multiple Wheels.- Each
Carhas oneEngineand eachEnginebelongs to oneCar. - Each
Carcontains multipleWheelsand eachWheelbelongs to oneCar.
Directed Association
Directed Association
A directed association is a one-way (unidirectional) relationship where one class points to another, indicating that one class uses or interacts with another.Example: A Another example: A
Driver drives a Car. The association is directed from Driver to Car — the Driver uses the Car, but the car does not necessarily belong to the driver.Teacher teaches a Course. The association is directed from Teacher to Course.Aggregation
Aggregation
Aggregation represents a part-of relationship where one class is a whole and another is a part. The part can exist independently of the whole (hollow diamond Another example: A
◇).Example: A Team consists of multiple Players. If the Team is deleted, the Players can still exist independently.Department has many Employees. Employees exist independently of the department.Composition
Composition
Composition is a strong part-of relationship where the part cannot exist independently of the whole (filled diamond Another example: A
◆). If the whole is deleted, the parts are also deleted.Example: A House consists of multiple Rooms. If the House is destroyed, the Rooms are also destroyed.Book has many Chapters. If the book is deleted, the chapters are also deleted.Generalization (Inheritance)
Generalization (Inheritance)
Generalization (also known as inheritance) represents an is-a relationship where one class (subclass/child) extends another (superclass/parent). The subclass inherits the attributes and methods of the superclass.Example: A
Bird is a type of Animal, and a Fish is also a type of Animal.Realization (Interface)
Realization (Interface)
Realization represents a contract between a class and an interface. A class that implements an interface must provide concrete implementations for all methods defined in the interface.Example: A
Shape interface defines draw() and area(), and Circle and Rectangle implement this interface.Dependency
Dependency
Dependency represents a depends-on relationship where one class (client) relies on another (supplier) to function. It is a weak relationship — the client uses the supplier temporarily (e.g., as a method parameter), not as a permanent attribute.Example: A
Dependency is considered loosely coupled — the client uses the supplier temporarily and does not maintain a permanent reference. However, changes to the supplier’s interface (method signatures) can still require updates to the client.
ReportGenerator uses a Printer to print reports.Usage Dependency
Usage Dependency
Usage Dependency is a type of dependency where one class uses another for a specific temporary purpose — usually within a method. The client does not maintain a permanent reference to the supplier.Example: A
Mechanic repairs a Car. The Mechanic uses the Car to perform repairs, but the Car does not depend on the Mechanic.Full Example: Order Management System
The following diagram demonstrates all relationship types in a realistic Order Management System.Generalization
RegisteredCustomer and GuestCustomer both inherit from Customer, sharing common attributes (id, name, email) while adding their own.Realization
CreditCard and PayPal implement the PaymentMethod interface, both providing a pay(amount: float): bool method.Association
One customer can place multiple orders; each order is placed by exactly one customer.
Composition
Each order has one
ShippingInfo. If the order is deleted, its shipping info is also deleted.Aggregation
An order contains multiple
OrderItems, but items can exist independently of the order. Each item refers to a Product that also exists independently.Dependency
An
Invoice is generated from an Order, but the invoice does not own the order. An Order uses a PaymentMethod to process payment but does not own it.