Class Diagram
UML & System Modelling
Imagine you're about to build a complex feature, like a new order management system. You have a team of developers, and you need to ensure everyone has a shared understanding of the core components: What are the main entities? What data do they hold? How do they relate to each other?
If you just start coding, each developer might have a slightly different mental model. This leads to integration problems, duplicated logic, and a system that's hard to understand and maintain. You need a blueprint.
A Class Diagram is that blueprint. It's a static, structural diagram in the Unified Modeling Language (UML) that describes the structure of a system by showing its classes, their attributes, operations (or methods), and the relationships among them. It is the most common and foundational diagram for Low-Level Design.
For an interview, being able to quickly sketch a Class Diagram on a whiteboard is a critical skill. It demonstrates that you can think about the structure of a system in a formal, standardized way before you start writing code.
Core Components of a Class Diagram
A class in a UML diagram is represented by a rectangle divided into three compartments:
- Top Compartment: Contains the name of the class. It should be centered and in bold.
- Middle Compartment: Contains the attributes (or fields) of the class.
- Bottom Compartment: Contains the operations (or methods) of the class.
Visibility
Each attribute and operation has a visibility marker, which specifies its access level:
+
Public: Accessible from any other class.-
Private: Accessible only within the class itself.#
Protected: Accessible within the class and its subclasses.~
Package Private: Accessible only by classes in the same package.
Relationships
Class Diagrams supports various types of relationships between classes. Here are the most common ones:
1. Association
An association represents a relationship between two or more classes. It's a "has-a" relationship. It can be bi-directional (without arrows) or uni-directional (with arrows). For example, a User
"has a" ShoppingCart
.
- Multiplicity: This defines how many instances of one class are linked to one instance of the other class.
1
: Exactly one*
: Many (zero or more)0..1
: Zero or one1..*
: One or more
2. Aggregation & Composition (Special Associations)
Both are special associations that represent a "whole-part" relationship, but they have a difference in their lifecycle dependency.
- Aggregation (Shared): A weaker "has-a" relationship where the "part" can exist independently of the "whole". For example, a
Department
hasProfessor
s, but if theDepartment
is closed, theProfessor
s still exist. It's depicted with an unfilled diamond.
- Composition (Owned): A stronger "has-a" relationship where the "part" cannot exist without the "whole". If the "whole" is destroyed, the "part" is also destroyed. For example, a
House
is composed ofRoom
s. If you demolish theHouse
, theRoom
s cease to exist. It's depicted with a filled diamond.
3. Inheritance (Generalization)
An "is-a" relationship. One class (the subclass or child) inherits from another class (the superclass or parent). For example, a Car
is a type of Vehicle
. It's depicted with a solid line and a hollow arrowhead pointing to the superclass.
4. Realization (Implementation)
A relationship between a class and an interface, where the class implements the operations specified by the interface. For example, a Car
class might implement the Drivable
interface. It's depicted with a dashed line and a hollow arrowhead pointing to the interface.
Example: E-Commerce System
Let's put it all together to model a simple e-commerce system.
Requirements:
- A
Customer
can have oneShoppingCart
. - A
ShoppingCart
can contain multipleLineItem
s. - Each
LineItem
is associated with exactly oneProduct
. - A
Customer
places anOrder
. - An
Order
is composed of theLineItem
s from the cart at the time of purchase. CreditCardPayment
andPayPalPayment
are different types ofPayment
methods.
This diagram clearly communicates the static structure of the system. Any developer can look at this and understand the core entities, their properties, and how they relate to one another before a single line of code is written. It's a powerful tool for communication and design clarity.