What is Inheritance in OOP?
Learn inheritance in OOP — is-a relationships, superclass and subclass, method overriding — with Java examples and common interview questions.
Expected Interview Answer
Inheritance is the object-oriented mechanism by which a class (the subclass) acquires the fields and methods of another class (the superclass), enabling code reuse and specialization.
A subclass automatically gets the non-private members of its parent and can add new members or override existing behavior to specialize it. This models an "is-a" relationship — a Dog IS AN Animal — and lets shared logic live in one place instead of being duplicated across related classes. Most languages support single inheritance of implementation (one direct parent) but allow multiple interface inheritance. Overused or misused inheritance creates fragile, tightly coupled hierarchies, which is why "favour composition over inheritance" is common guidance.
- Code reuse across related classes
- Models natural is-a hierarchies
- Enables polymorphism via a common base type
- Centralizes shared logic for easier maintenance
AI Mentor Explanation
An all-rounder inherits the base skill set expected of any player — fitness, fielding, understanding the rules — and then adds specialized batting and bowling technique on top. They don’t relearn fielding from scratch; they extend what every cricketer already has. That is inheritance: the subclass (all-rounder) reuses the base class (player) member fields and behaviors, and adds its own on top, without duplicating the shared logic.
Step-by-Step Explanation
Step 1
Identify the shared base
Find fields and behavior common to several related classes.
Step 2
Define the parent class
Put the shared members in a base class (e.g. Animal).
Step 3
Extend in the subclass
Use extends (Java/C++) so the child acquires the parent’s members.
Step 4
Add or override
The subclass adds new members and overrides methods it needs to specialize.
What Interviewer Expects
- A clear is-a relationship example
- Mention of single vs multiple inheritance support in the language
- Awareness that overriding is how subclasses specialize behavior
- Knowledge that inheritance can be misused (favour composition when appropriate)
Common Mistakes
- Using inheritance purely for code reuse without a real is-a relationship
- Forgetting that constructors are not inherited (must be called via super)
- Assuming Java supports multiple class inheritance (it does not)
- Not knowing private members are not directly accessible in the subclass
Best Answer (HR Friendly)
“Inheritance lets one class reuse the fields and methods of another class, so related types share common code instead of duplicating it. A subclass automatically gets everything the parent defines and can add or specialize behavior on top, which keeps code organized and easier to maintain.”
Code Example
class Animal {
protected String name;
Animal(String name) { this.name = name; }
void makeSound() { System.out.println(name + " makes a sound"); }
}
class Dog extends Animal {
Dog(String name) { super(name); }
@Override
void makeSound() { System.out.println(name + " barks"); }
}
Animal a = new Dog("Rex");
a.makeSound(); // Rex barksFollow-up Questions
- What is the difference between single and multiple inheritance?
- Why doesn’t Java support multiple class inheritance?
- How do you call a parent class constructor from a subclass?
- What is the difference between inheritance and composition?
MCQ Practice
1. Inheritance primarily models which relationship?
Inheritance expresses an is-a relationship — a subclass is a specialized kind of its parent.
2. In Java, how many classes can a single class directly extend?
Java supports single class inheritance — one direct superclass — but multiple interfaces.
3. What keyword calls the parent class constructor in Java?
super(...) invokes the superclass constructor from the subclass constructor.
Flash Cards
Inheritance in one line? — A subclass acquires the fields and methods of a parent class.
What relationship does it model? — is-a — e.g. Dog is an Animal.
How does a subclass specialize behavior? — By overriding inherited methods or adding new ones.
Common pitfall? — Overusing inheritance instead of favouring composition, causing fragile hierarchies.