Skip to content
Low Level Design Mastery Logo
LowLevelDesign Mastery

Intro to Class Relationships

Understand how classes relate to each other in object-oriented design.

Class relationships define how classes interact with each other in an object-oriented system. Understanding these relationships is crucial for designing maintainable, flexible, and well-structured software.

Class relationships help you:

  • Model real-world relationships accurately
  • Design flexible systems that can evolve
  • Communicate design clearly with diagrams
  • Avoid common pitfalls in system design
  • Write maintainable code with proper coupling

There are four main types of class relationships, ordered from weakest to strongest:

  1. Dependency - “Uses temporarily” (weakest)
  2. Association - “Uses-A” or “Knows-A”
  3. Aggregation - “Has-A” (weak ownership)
  4. Composition - “Has-A” (strong ownership)
Diagram
  1. “Does Class A use Class B temporarily?”

  2. “Does Class A know about Class B?”

  3. “Does Class A contain Class B?”

  4. “Can Class B exist without Class A?”

Diagram
  • Dependency: No ownership - temporary use
  • Association: No ownership - independent lifecycles
  • Aggregation: Weak ownership - parts can exist independently
  • Composition: Strong ownership - parts cannot exist without whole

Relationships can have different multiplicities:

  • 1 - Exactly one
  • 0..1 - Zero or one (optional)
  • 1..* - One or more
  • * - Many (zero or more)

Relationships can be:

  • Unidirectional - One class knows about another
  • Bidirectional - Both classes know about each other
  • Order uses Calculator temporarily to calculate total
  • Calculator is passed as parameter, not stored
  • Teacher teaches Course
  • Teacher knows about Course, Course can exist without Teacher
  • University has Students
  • Students can exist without University (can transfer)
  • Car has Engine
  • Engine cannot exist without Car (destroyed when Car is destroyed)

Now that you understand the basics, explore each relationship type in detail: