Java Inheritance: IS-A Hierarchy, 'extends' & the 'super' Keyword

Inheritance is an essential pillar of Object-Oriented Programming that allows a new class (known as the child / subclass / derived class) to inherit the state fields and behavioral methods of an existing class (known as the parent / superclass / base class).

The 3 Supported Types of Class Inheritance in Java

  • 1. Single Inheritance: A single subclass inherits from a single superclass (B extends A).
  • 2. Multilevel Inheritance: A class derives from a child class, creating a chain of inheritance (C extends B, and B extends A).
  • 3. Hierarchical Inheritance: Multiple distinct subclasses inherit from a single parent class (B extends A and C extends A).

Why Multiple Class Inheritance is Forbidden: The Diamond Problem

Suppose Class B and Class C both inherit from Class A and override display(). If Class D were permitted to extend both B and C, the compiler would have no way to determine which version of display() to inherit. To preserve language simplicity and safety, Java prohibits multiple class inheritance.

Practical Demonstration: Inheritance with the super Keyword

Let us examine a vehicle hierarchy showcasing extends and the three roles of super:

// Superclass (Parent)
class Vehicle {
    protected String registrationNumber;
    protected int maxSpeed;

    // Parent Constructor
    public Vehicle(String regNum, int maxSpeed) {
        this.registrationNumber = regNum;
        this.maxSpeed = maxSpeed;
    }

    public void startEngine() {
        System.out.println("Vehicle engine ignited. Max speed: " + maxSpeed + " km/h");
    }
}

// Subclass (Child)
class ElectricCar extends Vehicle {
    private int batteryCapacityKwh;

    public ElectricCar(String regNum, int maxSpeed, int batteryCapacity) {
        // 1. Invoking Parent Constructor using super()
        super(regNum, maxSpeed);
        this.batteryCapacityKwh = batteryCapacity;
    }

    // Overriding Parent Method
    @Override
    public void startEngine() {
        // 2. Invoking Parent's Original Method using super.
        super.startEngine();
        System.out.println("Electric inverter active! Battery: " + batteryCapacityKwh + " kWh");
    }
}

public class InheritanceDemo {
    public static void main(String[] args) {
        ElectricCar tesla = new ElectricCar("MH-12-EV-9999", 250, 85);
        tesla.startEngine();
    }
}

University Exam Corner (BCA / B.Sc CS)

Exam Question: "What is the ultimate root class of all classes in Java?"

Answer: java.lang.Object! Every class in Java directly or indirectly inherits from Object. If a class does not explicitly write extends, the compiler automatically inserts extends Object. This guarantees that all objects inherit foundational methods like toString(), equals(), hashCode(), and getClass().

Next Step

Now let us explore Polymorphism, Dynamic Method Dispatch, and Runtime Overriding. Proceed to: Java Polymorphism & Overriding →

Frequently Asked Questions (FAQ)

Q1. What is Inheritance in Java and what are its primary benefits?
Inheritance is an OOP mechanism where a new class (subclass/child) derives properties and methods from an existing class (superclass/parent) using the 'extends' keyword. It delivers two massive benefits: code reusability (DRY principle) and runtime polymorphism.
Q2. Why does Java not support Multiple Inheritance with classes?
Java bans multiple class inheritance to prevent the famous 'Diamond Problem' (ambiguity when two parent classes implement the same method with different logic). Java cleanly achieves multiple inheritance through Interfaces.
Q3. What are the 3 distinct uses of the 'super' keyword in Java?
The 'super' keyword is used to: 1. Invoke the superclass constructor (super()), 2. Refer to an instance variable of the immediate parent class when shadowed, and 3. Invoke an overridden method of the parent class (super.methodName()).

Explore Related Java Tutorials

Accelerate your Java mastery by studying these tightly connected core tutorials:

Polymorphism & Overriding →

Dynamic method dispatch.

Java Interfaces →

Multiple inheritance via interfaces.

Java Constructors →

Superclass constructor calls.

Access Modifiers →

Protected and public inheritance.

Share Share on Facebook Share on Twitter Share on LinkedIn Pin on Pinterest Share on Stumbleupon Share on Tumblr Share on Reddit Share on Diggit

You may also like this!