Skip to content
LowLevelDesign Mastery

Class Diagrams

Master class diagrams - visualize your design and communicate effectively in LLD interviews!

Class diagrams are the visual representation of your design. They help you:

  • Communicate your design to interviewers
  • Visualize relationships between classes
  • Identify design issues early
  • Plan implementation
Why class diagrams matter: a text description of parking lot relationships visualized as a clear class diagram

Class diagrams show:

A class in a diagram shows:

  • Class Name - At the top
  • Attributes - Properties/fields (middle section)
  • Methods - Operations (bottom section)
UML class notation: class name, attributes and methods sections with private, public and protected access modifiers
  • + Public - Accessible from anywhere
  • - Private - Only accessible within class
  • # Protected - Accessible within class and subclasses
  • ~ Package - Accessible within package

  1. Inheritance (is-a) - Generalization
  2. Composition (has-a, owns) - Strong ownership
  3. Aggregation (has-a, uses) - Weak ownership
  4. Association (uses) - Reference/usage
  5. Dependency (depends on) - Temporary usage
Five UML class relationship types: inheritance, composition, aggregation, association and dependency with parking lot and team examples

Inheritance represents an “is-a” relationship. A subclass is a type of superclass.

Example: Car is a Vehicle

  • Solid line with hollow triangle pointing to parent
  • Arrow: Child → Parent
Inheritance example: abstract Vehicle class extended by Car, Motorcycle and Truck

✔ Use when:

  • Subclass is a type of superclass
  • Subclass shares common behavior
  • You want polymorphism

❌ Don’t use when:

  • Just sharing code (use composition)
  • Relationship is “has-a” not “is-a”

Composition represents a “has-a” relationship with strong ownership. The whole owns the parts, and parts cannot exist without the whole.

Example: ParkingLot has ParkingSpots (spots don’t exist without the lot)

  • Solid line with filled diamond on the whole side
  • Arrow: Part → Whole
  • Multiplicity: 1 to many
Composition example: ParkingLot owns its ParkingSpots, shown with a diamond on the ParkingLot side

✔ Use when:

  • Whole owns parts
  • Parts cannot exist without whole
  • Lifecycle is tied together
  • Strong relationship

Example: Car has Engine (engine doesn’t exist without car)


Aggregation represents a “has-a” relationship with weak ownership. The whole uses the parts, but parts can exist independently.

Example: Team has Players (players exist without team)

  • Solid line with hollow diamond on the whole side
  • Arrow: Part → Whole
  • Multiplicity: 1 to many
Aggregation example: Team has Players with add and remove methods, a weak ownership relationship

✔ Use when:

  • Whole uses parts
  • Parts can exist independently
  • Lifecycle is independent
  • Weak relationship

Example: Library has Books (books exist without library)

AspectCompositionAggregation
OwnershipStrongWeak
LifecycleTied togetherIndependent
ExampleCar-EngineTeam-Player
DiamondFilledHollow

Association represents a “uses” relationship. One class references or uses another class.

Example: Ticket references Vehicle

  • Solid line with arrow (optional)
  • Arrow: Source → Target
  • Can have multiplicity
Association example: Ticket references a Vehicle and a ParkingSpot

✔ Use when:

  • One class uses another
  • Reference relationship
  • Not ownership
  • Temporary or optional

Example: Order references Customer (order uses customer info)


Dependency represents a temporary “depends on” relationship. One class depends on another for a specific operation.

Example: Method parameter, return type, local variable

  • Dashed line with arrow
  • Arrow: Dependent → Dependency
Dependency example: ParkingLotService uses ParkingSpotRepository to find available spots and save them

✔ Use when:

  • Temporary relationship
  • Method parameter
  • Return type
  • Local variable usage

Full parking lot class diagram showing composition, dependency, association and inheritance between lot, spot, vehicle, ticket and payment
RelationshipTypeExample
ParkingLot → ParkingSpotCompositionParkingLot has ParkingSpots (owns)
Vehicle → CarInheritanceCar is a Vehicle
Ticket → VehicleAssociationTicket references Vehicle
Payment → TicketAssociationPayment references Ticket
ParkingLot → TicketDependencyParkingLot creates Ticket

Multiplicity shows how many instances participate in a relationship.

  • 1 - Exactly one
  • 0..1 - Zero or one (optional)
  • 1..* - One or more
  • * or 0..* - Zero or more (many)
  • n..m - Between n and m
Multiplicity example: ParkingLot has many spots, each spot holds zero or one vehicle, each ticket references one vehicle and one spot

Interfaces define contracts that classes implement.

UML Notation: <<interface>> or I prefix

Interface example: PaymentProcessor interface implemented by StripeProcessor and PayPalProcessor

Abstract classes provide partial implementation.

UML Notation: <<abstract>> or italic name

Abstract class example: abstract Vehicle with parking fee calculation extended by Car

✔ Use clear names - Descriptive class and method names
✔ Show key relationships - Don’t show everything, focus on important ones
✔ Use proper notation - Follow UML standards
✔ Group related classes - Organize diagram logically
✔ Show multiplicities - When important
✔ Use interfaces - For abstraction

❌ Don’t show everything - Focus on important relationships
❌ Don’t use wrong relationships - Understand composition vs aggregation
❌ Don’t make it too complex - Keep it readable
❌ Don’t forget access modifiers - Show visibility
❌ Don’t mix concepts - Keep layers separate

Good vs bad class diagram: clear relationships, proper notation and focus versus wrong relationships and messy complexity

✔ Class diagrams visualize your design
✔ Inheritance - “is-a” relationship (solid line, hollow triangle)
✔ Composition - “has-a” with ownership (filled diamond)
✔ Aggregation - “has-a” without ownership (hollow diamond)
✔ Association - “uses” relationship (solid line)
✔ Dependency - “depends on” (dashed line)
✔ Multiplicity - Shows quantity in relationships
✔ Interfaces - Define contracts

RelationshipNotationWhen to Use
InheritanceSolid line, hollow triangle”is-a” relationship
CompositionSolid line, filled diamondStrong ownership
AggregationSolid line, hollow diamondWeak ownership
AssociationSolid line, arrowReference/usage
DependencyDashed line, arrowTemporary dependency
Summary: visualizing your design as a class diagram of classes, relationships, interfaces and multiplicities for clear communication

Now that you’ve mastered class diagrams, let’s learn how to define contracts and APIs:

Next: Contract and API Definitions →

This next guide will teach you how to define clear interfaces, method signatures, and API contracts for your design!


💡 Time to Practice!

Now that you understand the concepts, put them into practice with our interactive playground. Build UML diagrams, write code, and get AI-powered feedback.

Browse All Problems