Skip to content
Low Level Design Mastery Logo
LowLevelDesign Mastery

Composition

Strong ownership - parts cannot exist without the whole.

Composition is a “has-a” relationship where one class contains another, and the contained class cannot exist independently. It represents a strong ownership relationship with tied lifecycles.

Composition represents:

  • “Has-a” relationship (strong ownership)
  • Parts cannot exist independently of the whole
  • Parts belong to only one whole
  • Lifecycle dependency - parts are destroyed when whole is destroyed
  • Strong ownership - Container owns the parts
  • Dependent lifecycle - Parts cannot exist without container
  • Single ownership - Parts belong to only one container
  • Filled diamond in UML diagrams
Diagram
FeatureCompositionAggregation
OwnershipStrongWeak
LifecycleDependentIndependent
Multi-ownershipNoYes
UML SymbolFilled diamondHollow diamond
ExampleCar → EngineUniversity → Students

Use Composition when:

  • Parts cannot exist without the whole
  • Parts belong to only one whole
  • Lifecycle is tied together
  • Relationship is essential and permanent
  • Parts are created by the whole

Examples:

  • Car → Engine (engine doesn’t exist without car)
  • Order → OrderItems (items don’t exist without order)
  • House → Room (rooms don’t exist without house)
  • Document → Paragraph (paragraphs don’t exist without document)
  • Computer → CPU (CPU doesn’t exist without computer)

💡 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