AlgoMaster Logo

Composition

Ashish

Ashish Pratap Singh

What if a relationship is so strong that the "part" is meaningless and cannot even exist without the "whole"?

This is the world of Composition.

Composition is the strongest form of association in object-oriented design. It models a “has-a” relationship between a whole and its parts—but unlike aggregation, composition comes with ownership and lifecycle dependency.

When you use composition, you're saying:

  • “This object is composed of other objects.”
  • “And if the container goes away, so do its parts.”

1. What is Composition?

Composition is a special type of association that signifies strong ownership between objects. The “whole” class is fully responsible for creating, managing, and destroying the “part” objects. In fact, the parts cannot exist without the whole.

Key Characteristics of Composition:

  • Represents a strong “has-a” relationship.
  • The whole owns the part and controls its lifecycle.
  • When the whole is destroyed, the parts are also destroyed.
  • The parts are not shared with any other object.
  • The part has no independent meaning or identity outside the whole.

If the part makes no sense without the whole, use composition.

2. Code Example

Let’s model above example using code.

Room class

House class

Explanation

  • The House creates, manages, and owns its Room objects.
  • The Room objects do not exist independently outside the context of the House.
  • No external class should reuse or manage these Room instances.
  • If the House is deleted (e.g., garbage collected), the Rooms are destroyed too.

This demonstrates a true composition relationship—where object ownership and lifecycle are tightly coupled.

3. UML Representation

In UML class diagrams, composition is represented by a filled diamond (◆) at the “whole” end of the relationship.

This means:

  • A House owns multiple Rooms.
  • The Rooms do not exist independently.
  • When the House dies, the Rooms die with it.

4. When to Use Composition

Use composition when:

  • The part is not meaningful without the whole.
  • The whole should control the lifecycle of its parts.
  • The parts are not reused elsewhere in the system.
  • You want to model a strong containment relationship.

Composition is a preferred alternative to inheritance when building flexible systems.

Favor composition over inheritance.” — GoF Design Principle

Why?

  • You can build complex behavior by composing smaller, reusable parts.
  • It avoids the tight coupling and fragility of inheritance hierarchies.
  • You can swap out parts dynamically to modify behavior.

For example:

  • A Vehicle can compose an Engine interface.
  • Swap between PetrolEngine, ElectricEngine, or HybridEngine at runtime.

This leads to cleaner, testable, and decoupled code.

5. Composition vs Aggregation vs Association