🧬 Inheritance in Java — extends, super and Method Overriding
Master Java inheritance — extends, the super keyword, method overriding, constructor chaining, the Object class and why Java has no multiple class inheritance. With examples.
Inheritance lets a class reuse and extend another. Java uses the extends keyword and supports single class inheritance (one direct superclass).
📜 The pattern
class Animal {
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); } // call parent constructor
@Override
void speak() { System.out.println(name + " barks"); }
}🔑 The super keyword
super(...)— call the parent constructor (must be the first statement).super.method()— call the parent's version of an overridden method.
🎭 Method overriding
A subclass redefines a superclass method with the same signature. Always annotate with @Override — the compiler verifies you're really overriding. Calls are resolved at runtime based on the object's actual type (dynamic dispatch).
🌳 The Object class
Every class implicitly extends Object, inheriting equals(), hashCode(), toString() — which you'll often override.
⚠️ No multiple class inheritance
Java forbids extending two classes (avoids the diamond problem). Use interfaces for multiple type inheritance — covered next. Coming from C++? Compare with the inheritance topic in our C++ course.
💻 Code Examples
Override with @Override + super
class Vehicle {
void describe() { System.out.println("A vehicle"); }
}
class Car extends Vehicle {
@Override void describe() {
super.describe();
System.out.println("...specifically a car");
}
}
new Car().describe();A vehicle ...specifically a car
Constructor chaining
class Base { Base() { System.out.println("Base ctor"); } }
class Derived extends Base {
Derived() { System.out.println("Derived ctor"); }
}
new Derived();Base ctor Derived ctor
⚠️ Common Mistakes
- Forgetting @Override — a typo in the method name silently creates a NEW method instead of overriding.
- Putting super(...) anywhere but the first line of the constructor — compile error.
- Trying to extend two classes — Java allows only single class inheritance; use interfaces.
- Overriding and accidentally narrowing access (e.g., public → protected) — not allowed.
🎯 Interview Questions
Real questions asked at top product and service-based companies.
Q1.What is inheritance and what keyword implements it?Beginner
Q2.What does super do?Beginner
Q3.What's the difference between overriding and overloading?Intermediate
Q4.Why doesn't Java support multiple class inheritance?Intermediate
Q5.What methods does every class inherit from Object?Advanced
🧠 Quick Summary
- extends gives single class inheritance (one superclass).
- super(...) chains constructors; super.method() calls parent code.
- Override with the same signature; always annotate @Override.
- Overriding is resolved at runtime (dynamic dispatch).
- No multiple class inheritance — use interfaces; every class extends Object.