Class Diagram

Visualize class structure and relationships

Overview

Class diagrams are the most common UML diagrams used in LLD. They show the static structure of a system by displaying classes, their attributes, methods, and relationships between them.

Class diagrams are essential for designing and documenting object-oriented systems.

Key Concepts

Classes shown as rectangles with three compartments

Top: Class name, Middle: Attributes, Bottom: Methods

Access modifiers: + (public), - (private), # (protected)

Relationships: Association, Aggregation, Composition, Inheritance

Multiplicity: 1, 0..1, , 1.., etc.

Code Example

java
1/*
2Class Diagram Notation:
3┌─────────────────────────────┐
4│         <<interface>>       │  ← Stereotype (optional)
5│           Vehicle           │  ← Class name
6├─────────────────────────────┤
7│ - brand: String             │  ← Attributes (- = private)
8│ - model: String             │
9│ # year: int                 │  ← # = protected
10├─────────────────────────────┤
11│ + start(): void             │  ← Methods (+ = public)
12│ + stop(): void              │
13│ + getInfo(): String         │
14└─────────────────────────────┘
15
16Relationships:
17─────────────────> Association (simple line with arrow)
18◇─────────────────> Aggregation (hollow diamond)
19◆─────────────────> Composition (filled diamond)  
20─ ─ ─ ─ ─ ─ ─ ─ ─ ▷ Realization/Interface (dashed with hollow arrow)
21────────────────────▷ Inheritance (solid with hollow arrow)
22- - - - - - - - - -> Dependency (dashed arrow)
23
24Example: E-commerce System
25┌─────────────────┐       ┌─────────────────┐
26│     Customer    │       │     Product     │
27├─────────────────┤       ├─────────────────┤
28│ - id: String    │       │ - id: String    │
29│ - name: String  │       │ - name: String  │
30│ - email: String │       │ - price: double │
31├─────────────────┤       ├─────────────────┤
32│ + placeOrder()  │       │ + getDetails()  │
33└────────┬────────┘       └────────┬────────┘
34         │ 1                       │ *
35         │                         │
36         ▼ *                       │
37┌─────────────────┐                │
38│      Order      │◆───────────────┘
39├─────────────────┤    (Composition)
40│ - orderId       │
41│ - orderDate     │
42│ - totalAmount   │
43├─────────────────┤
44│ + addItem()     │
45│ + checkout()    │
46└─────────────────┘
47*/

This shows UML class diagram notation including classes, attributes, methods, and different types of relationships.

Best Practices

  • 1.

    Keep diagrams focused - don't include everything

  • 2.

    Use proper notation for relationships

  • 3.

    Include multiplicity on associations

  • 4.

    Show only relevant attributes and methods

  • 5.

    Group related classes visually

💡 Interview Tips

  • Draw class diagrams during LLD interviews

  • Know all relationship types and their notation

  • Start with main entities, then add relationships

  • Use proper access modifiers

  • Practice drawing common systems (parking lot, library)

AI Tutor

Ask about the topic

Sign in Required

Please sign in to use the AI tutor

Sign In
Class Diagram - UML | LLD | Revise Algo