100% Free Forever
AI-Powered Learning
Industry Expert Content
Certificates & Badges
Learn At Your Own Pace

Composition vs Inheritance in OOP

Composition vs inheritance in OOP — has-a vs is-a, coupling, the fragile base class problem and why to favour composition — with Java examples and Q&A.

mediumQ5 of 226 in Object Oriented Programming Est. time: 5 minsLast updated:
Open Code Lab

Expected Interview Answer

Inheritance models an "is-a" relationship where a class derives from a parent, while composition models a "has-a" relationship where a class is built from other objects it holds as members; the common guidance is to favour composition over inheritance.

Inheritance reuses code by extending a base class, but it creates tight coupling: subclasses depend on the parent’s implementation, and deep hierarchies become fragile (the "fragile base class" problem). Composition builds behavior by delegating to contained objects, giving looser coupling and more flexibility — you can swap parts at runtime and avoid rigid hierarchies. Use inheritance for genuine is-a specialization; prefer composition for reuse and assembling behavior.

  • Composition: loose coupling and runtime flexibility
  • Composition: avoids fragile deep hierarchies
  • Inheritance: clean for genuine is-a specialization

AI Mentor Explanation

Inheritance is "a fast bowler IS A bowler" — a specialization of a base type. Composition is "a team HAS A captain, a coach, a physio" — assembled from parts it holds. You can swap the physio without redefining the team (flexible composition), but changing the base "bowler" ripples to every subclass (rigid inheritance). Prefer building teams from swappable parts over deep rigid hierarchies — favour composition.

Step-by-Step Explanation

  1. Step 1

    Inheritance = is-a

    A subclass extends a base class, reusing and specializing it.

  2. Step 2

    Composition = has-a

    A class holds other objects and delegates behavior to them.

  3. Step 3

    Coupling

    Inheritance couples subclass to parent; composition stays loosely coupled.

  4. Step 4

    Guideline

    Favour composition for reuse; use inheritance for true specialization.

What Interviewer Expects

  • is-a vs has-a distinction
  • Why "favour composition over inheritance" is common advice
  • The fragile base class / tight coupling problem
  • When inheritance is still the right choice

Common Mistakes

  • Using inheritance purely for code reuse (not is-a)
  • Creating deep, fragile inheritance hierarchies
  • Thinking composition and inheritance are mutually exclusive
  • Not recognizing tight coupling from inheritance

Best Answer (HR Friendly)

Inheritance is an "is-a" relationship — a subclass extends a parent. Composition is a "has-a" relationship — a class is built from other objects it contains. Composition is usually preferred because it’s more flexible and loosely coupled, while inheritance suits genuine specialization.

Code Example

Composition (has-a) vs inheritance (is-a)
// Inheritance: Car IS-A Vehicle
class Vehicle { void move() {} }
class Car extends Vehicle {}

// Composition: Car HAS-A Engine (swappable at runtime)
class Engine { void start() {} }
class Car2 {
    private Engine engine;
    Car2(Engine engine) { this.engine = engine; }  // inject the part
    void start() { engine.start(); }                // delegate
}

Follow-up Questions

  • Why is composition often preferred over inheritance?
  • What is the fragile base class problem?
  • How does dependency injection relate to composition?
  • Give an example where inheritance is the better choice.

MCQ Practice

1. Composition models which relationship?

Composition means a class HAS-A (contains) other objects it delegates to.

2. A key downside of deep inheritance is?

Subclasses depend on the parent’s implementation, making hierarchies fragile.

3. The common design guideline is?

Favour composition over inheritance for flexibility and looser coupling.

Flash Cards

Inheritance relationship?"is-a" — a subclass extends and specializes a parent.

Composition relationship?"has-a" — a class holds and delegates to other objects.

Common guideline?Favour composition over inheritance.

Inheritance downside?Tight coupling and fragile deep hierarchies.

1 / 4

Continue Learning