Complete guide to UML Class Diagrams: learn how to model system structure with classes, attributes, methods, and relationships. Master the most fundamental UML diagram type for object-oriented design.
Table of Contents
1. What are Class Diagrams?
Class diagrams are the most common and fundamental UML diagrams. They show the static structure of a system by depicting classes, their attributes, methods, and the relationships between classes.
Class diagrams are used throughout the software development lifecycle:
- Analysis: Understanding domain concepts and their relationships
- Design: Designing object-oriented systems and APIs
- Implementation: Guiding code structure and organization
- Documentation: Documenting system architecture and design decisions
A class diagram shows what classes exist, what attributes and methods they have, and how classes relate to each other. It does not show the dynamic behavior or the flow of execution.
2. Class Syntax and Notation
2.1 Basic Class Representation
A class is represented as a rectangle divided into three compartments:
- Name compartment: Contains the class name (required)
- Attributes compartment: Lists the class attributes (optional)
- Methods compartment: Lists the class methods/operations (optional)
2.2 Class Name
The class name should:
- Be a noun or noun phrase
- Start with an uppercase letter
- Use PascalCase (e.g., CustomerOrder, PaymentProcessor)
- Be descriptive and meaningful
2.3 Abstract Classes
Abstract classes cannot be instantiated directly. They are shown with the class name in italics or with the keyword {abstract}:
2.4 Interfaces
Interfaces define contracts that classes can implement. They are shown with the keyword <
3. Attributes
3.1 Attribute Syntax
Attributes are written in the format:
visibility name : type [multiplicity] [= default value]
Example:
- customerId : String
+ balance : Double = 0.0
- items : List<OrderItem> [1..*]
# status : OrderStatus
3.2 Attribute Components
- Visibility: + (public), - (private), # (protected), ~ (package)
- Name: Descriptive name in camelCase
- Type: Data type (String, Integer, Customer, etc.)
- Multiplicity: [1], [0..1], [1..*], [0..*], etc.
- Default Value: Initial value (optional)
3.3 Derived Attributes
Derived attributes are calculated from other attributes. They are shown with a forward slash (/):
+ totalPrice : Double
+ /discountedPrice : Double ← Derived attribute
3.4 Static Attributes
Static (class-level) attributes are shown with underline or {static}:
+ MAX_ITEMS : Integer {static}
+ instanceCount : Integer ← Underlined
4. Methods (Operations)
4.1 Method Syntax
Methods are written in the format:
visibility name(parameter1 : type1, parameter2 : type2) : returnType
Example:
+ calculateTotal() : Double
- validateOrder() : Boolean
+ addItem(item : OrderItem) : void
+ findById(id : String) : Order
4.2 Method Components
- Visibility: +, -, #, ~ (same as attributes)
- Name: Verb or verb phrase in camelCase
- Parameters: name : type pairs in parentheses
- Return Type: Data type returned (void if nothing)
4.3 Abstract Methods
Abstract methods have no implementation. They are shown in italics or with {abstract}:
+ calculatePrice() : Double {abstract}
+ processPayment() : void ← Italic
4.4 Static Methods
Static methods are shown with underline or {static}:
+ getInstance() : Singleton {static}
+ createDefault() : Order ← Underlined
5. Relationships
5.1 Association
Association represents a general relationship between classes. It's shown as a solid line connecting classes.
Properties:
- Can have role names (e.g., "placedBy", "hasOrders")
- Can have multiplicity (1, *, 1..*, 0..1)
- Can be bidirectional or unidirectional (arrow)
5.2 Aggregation
Aggregation represents a "has-a" relationship with weak ownership. The part can exist independently. Shown with a hollow diamond.
5.3 Composition
Composition represents a "has-a" relationship with strong ownership. The part cannot exist without the whole. Shown with a filled diamond.
5.4 Generalization (Inheritance)
Generalization represents an "is-a" relationship (inheritance). Shown with a hollow triangle arrow.
5.5 Realization (Implementation)
Realization represents a class implementing an interface. Shown with a dashed line and hollow triangle.
5.6 Dependency
Dependency represents a "uses" relationship where one class depends on another. Shown with a dashed arrow.
6. Visibility Modifiers
| Symbol | Name | Description |
|---|---|---|
| + | Public | Accessible from anywhere |
| - | Private | Accessible only within the class |
| # | Protected | Accessible within the class and subclasses |
| ~ | Package | Accessible within the same package |
7. Advanced Concepts
7.1 Multiplicity
Multiplicity specifies how many instances participate in a relationship:
- 1: Exactly one
- 0..1: Zero or one (optional)
- 1..*: One or more
- 0..* or *: Zero or more
- 2..5: Between 2 and 5
7.2 Constraints
Constraints specify rules that must be satisfied. Shown in curly braces:
{ordered} - Elements are ordered
{unique} - Elements are unique
{readOnly} - Cannot be modified
{immutable} - Cannot change after creation
7.3 Stereotypes
Stereotypes extend UML vocabulary. Shown with guillemets:
<>
<>
<>
<>
7.4 Notes
Notes provide additional information. Shown as a rectangle with a folded corner:
8. Best Practices
8.1 Naming Conventions
- Use PascalCase for class names (CustomerOrder)
- Use camelCase for attributes and methods (customerId, calculateTotal)
- Use descriptive names that clearly indicate purpose
- Avoid abbreviations unless widely understood
8.2 Organization
- Group related classes together
- Use packages to organize large diagrams
- Show only essential relationships to avoid clutter
- Create multiple diagrams for different views
8.3 Detail Level
- Show only relevant attributes and methods
- Use different diagrams for different audiences
- Hide implementation details when not needed
- Focus on public interface for high-level views
8.4 Relationships
- Use appropriate relationship types (don't use inheritance for "has-a")
- Show multiplicity when it's not obvious
- Use role names to clarify relationships
- Avoid circular dependencies
0 Comments