is-a vs has-a

This line is extremely important for interviews and real-world design.
Let’s break it down clearly, practically, and interview-ready.

“Inheritance models is-a, composition models has-a, and most real-world systems favor has-a.”


1️⃣ Inheritance models IS-A

Inheritance means one class is a specialized form of another.

Example

class Vehicle {
    void move() {}
}

class Car extends Vehicle {
}

👉 A Car IS A Vehicle

This relationship must be:

  • Always true
  • True in every context

If you say:

“Car is a Vehicle”

…and it sounds natural everywhere → inheritance may be OK.


❌ Problem with IS-A in real systems

Inheritance creates:

  • Tight coupling
  • Rigid hierarchies
  • Fragile designs

If Vehicle changes → Car may break.

This is why inheritance is powerful but dangerous.


2️⃣ Composition models HAS-A

Composition means one object contains another object and uses it.

Example

class Engine {
    void start() {}
}

class Car {
    private Engine engine;

    Car(Engine engine) {
        this.engine = engine;
    }
}

👉 A Car HAS AN Engine

Key points:

  • Car uses Engine
  • Car does not become Engine
  • Engine can be replaced

3️⃣ Why “Most Real-World Systems Favor HAS-A”

Because real-world behavior changes more than identity.

Real Life Analogy

  • A car may:
    • Change engine
    • Upgrade battery
    • Switch transmission

But:

A car never becomes an engine

So HAS-A is more realistic than IS-A.


4️⃣ Real Engineering Reason (Interview Gold)

Inheritance (IS-A)

❌ Compile-time binding
❌ Hard to change
❌ Deep hierarchies
❌ Violates Open/Closed Principle easily

Composition (HAS-A)

✅ Loose coupling
✅ Runtime flexibility
✅ Easier testing
✅ Better for scaling systems


5️⃣ Interview Favorite Example: Vehicle Design

❌ Bad Design (Inheritance Abuse)

class ElectricCar extends Car {}
class DieselCar extends Car {}
class HybridCar extends Car {}

What happens when:

  • Battery logic changes?
  • Engine logic changes?

👉 Explosion of subclasses


✅ Good Design (Composition)

interface Engine {
    void power();
}

class ElectricEngine implements Engine {}
class DieselEngine implements Engine {}

class Car {
    private Engine engine;
}

Now:

  • Add new engine → no change in Car
  • Follow Open/Closed Principle
  • Cleaner system

6️⃣ One-Line Interview Answer (MEMORIZE)

“Inheritance should be used only when the IS-A relationship is absolutely true and stable. In most systems, behavior changes more often than identity, so composition (HAS-A) is safer and more flexible.”

🔥 This answer impresses senior interviewers.


7️⃣ When Inheritance Is Acceptable

Use inheritance when:

  • Relationship is permanent
  • Subclass truly extends behavior
  • Base class is stable

Example

class Exception {}
class RuntimeException extends Exception {}

This is a true IS-A relationship.


8️⃣ Final Summary (Interview Ready)

ConceptInheritanceComposition
RelationshipIS-AHAS-A
CouplingTightLoose
FlexibilityLowHigh
Preferred in real systems

💡 Final Thought

Inheritance is about identity.
Composition is about capability.
Real systems evolve by changing capabilities, not identities.


Read other awesome articles in Medium.com or in akcoding’s posts.

OR

Join us on YouTube Channel

OR Scan the QR Code to Directly open the Channel 👉

AK Coding YouTube Channel

Share with