Intermediate⏱️ 10 min📘 Topic 11 of 22

🧬 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();
Output:
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();
Output:
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
Inheritance lets a subclass reuse and extend a superclass's fields and methods, modeling an 'is-a' relationship. Java uses the extends keyword.
Q2.What does super do?Beginner
super(...) calls the parent constructor (first statement in the child constructor). super.method() calls the parent's implementation of an overridden method.
Q3.What's the difference between overriding and overloading?Intermediate
Overriding: subclass redefines a parent method with the same signature, resolved at runtime (dynamic dispatch). Overloading: same method name, different parameters in the same class, resolved at compile time.
Q4.Why doesn't Java support multiple class inheritance?Intermediate
To avoid the diamond problem — ambiguity when two parents define the same method. Java allows a class to implement multiple interfaces instead, which provides multiple type inheritance without state ambiguity.
Q5.What methods does every class inherit from Object?Advanced
equals(), hashCode(), toString(), getClass(), clone(), wait()/notify(), finalize(). You commonly override equals, hashCode and toString.

🧠 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.