How Does Inheritance Work in Java?
Learn how inheritance works in Java with the extends keyword, super, and method overriding. Clear examples, interview tips, and common mistakes explained.
Expected Interview Answer
Inheritance in Java lets one class (the subclass) acquire the fields and methods of another class (the superclass) using the extends keyword, so shared behaviour is written once and reused.
The subclass inherits accessible members of its parent and can add new members or override inherited methods to specialise behaviour. Java supports single class inheritance only — a class extends exactly one superclass — but a class can implement many interfaces. The super keyword calls the parent constructor or an overridden parent method, and every class ultimately inherits from java.lang.Object.
- Reuses code instead of duplicating it
- Models real-world is-a relationships
- Enables runtime polymorphism through overriding
- Makes code easier to extend and maintain
- Centralises shared logic in one base class
AI Mentor Explanation
A fast bowler and a spin bowler are both bowlers: they inherit the general skills of running in, delivering a legal ball, and following field placements. Each then specialises — the fast bowler adds seam movement, the spinner adds flight and turn. In Java the Bowler superclass holds the shared behaviour and each specific bowler extends it, overriding the delivery method to add its own action.
Step-by-Step Explanation
Step 1
Define the superclass
Write a base class holding the fields and methods common to a family of related types.
Step 2
Extend it
Declare the subclass with extends BaseClass so it inherits all accessible members automatically.
Step 3
Chain the constructor
Call super(...) as the first statement in the subclass constructor to initialise the inherited state.
Step 4
Add or override
Add new members for specialised behaviour, or override inherited methods with @Override to change them.
Step 5
Use polymorphically
Refer to subclass instances through the superclass type so the correct overridden method runs at runtime.
What Interviewer Expects
- Correct use of the extends keyword
- Understanding that Java allows only single class inheritance
- Role of super and constructor chaining
- Difference between overriding and overloading
- Awareness that every class extends Object
Common Mistakes
- Claiming Java supports multiple class inheritance
- Confusing method overriding with overloading
- Forgetting that super() must be the first statement in a constructor
- Trying to access private members of the superclass directly
- Overusing inheritance where composition would fit better
Best Answer (HR Friendly)
“Inheritance lets a new class reuse the features of an existing class instead of rewriting them, then add its own extras. In Java you write extends to build a specialised version of a general class, which keeps shared code in one place and easier to maintain.”
Code Example
class Animal {
protected String name;
Animal(String name) {
this.name = name;
}
void speak() {
System.out.println(name + " makes a sound");
}
}
class Dog extends Animal {
Dog(String name) {
super(name);
}
@Override
void speak() {
System.out.println(name + " barks");
}
}
public class Main {
public static void main(String[] args) {
Animal a = new Dog("Rex");
a.speak(); // Rex barks
}
}Follow-up Questions
- Why does Java not support multiple inheritance of classes?
- How do interfaces provide a form of multiple inheritance?
- What is the difference between overriding and overloading?
- When should you prefer composition over inheritance?
- What does the super keyword do?
MCQ Practice
1. Which keyword is used to inherit a class in Java?
A class inherits another class using the extends keyword; implements is used for interfaces.
2. How many superclasses can a Java class directly extend?
Java supports single class inheritance, so a class can directly extend only one superclass.
3. What must be the first statement in a subclass constructor that calls its parent constructor?
super(...) must be the first statement when explicitly invoking the superclass constructor.
Flash Cards
Which keyword creates inheritance? — extends — the subclass inherits accessible fields and methods of the superclass.
How many classes can Java extend at once? — Only one — Java supports single class inheritance but multiple interface implementation.
What does super do? — It calls the parent constructor or an overridden parent method from the subclass.
Root of every class hierarchy? — java.lang.Object — every class implicitly inherits from it.