What is Inheritance in Java and why is it important?

Share your love

Index

What is inheritance in Java?

Inheritance in Java
Inheritance in Java

How to Use Inheritance in Java?

public class Test {

    public static void main(String[] args) {

        Bicycle b = new Bicycle(50, 20);
        MountainBike mb = new MountainBike(3, 100, 25);

        System.out.println(mb.toString());
        System.out.println();
        System.out.println(b.toString());
    }
}

// Base class
class Bicycle {

    public int gear;
    public int speed;

    // Constructor of Bicycle
    public Bicycle(int g, int s) {
        gear = g;
        speed = s;
    }

    // Method to apply brake
    public void applyBrake(int decrement) {
        speed -= decrement;
    }

    // Method to speed up
    public void speedUp(int increment) {
        speed += increment;
    }

    // toString() method to print info of Bicycle
    public String toString() {
        return "No of gears are " + gear + "\n"
             + "Speed of bicycle is " + speed;
    }
}

// Derived class
class MountainBike extends Bicycle {

    // Field specific to MountainBike
    public int seatHeight;

    // Constructor of MountainBike
    public MountainBike(int g, int s, int startHeight) {
        // Invoking base-class (Bicycle) constructor
        super(g, s);
        seatHeight = startHeight;
    }

    // Method to set seat height
    public void setHeight(int newValue) {
        seatHeight = newValue;
    }

    // Overriding toString() method
    @Override
    public String toString() {
        return super.toString() + "\nSeat height is " + seatHeight;
    }
}

Output

Output of the code to show inheritance in Java
The output of the code to show inheritance in Java
public String toString() {
    return ("No of gears are " + gear + "\n" + "speed of bicycle is " + speed);
}
return (super.toString() + "\nseat height is " + seatHeight);

Types of Inheritance in Java

Types of inheritance in Java
Types of inheritance in Java

Single inheritance 

Single Inheritance in Java
Single inheritance in Java

Code Example

public class Main {

    public static void main(String[] args) {
        Dog d = new Dog();
        d.eat();
        d.bark();
    }
}

class Animal {

    void eat() {
        System.out.println("This animal eats food.");
    }
}

class Dog extends Animal {

    void bark() {
        System.out.println("The dog barks.");
    }
}

Output

Multilevel Inheritance 

Multilevel Inheritance in Java
Multilevel Inheritance in Java

Multilevel inheritance helps us avoid repetitive code and lets us build more specific classes based on general ones, making our programs easier to manage and scale.

Understanding multilevel Inheritance with a code

Example of multilevel inheritance in Java
Example of multilevel inheritance in Java
public class Test {

    public static void main(String[] args) {

        // Creating an object of MountainBike
        MountainBike mb = new MountainBike(20, 5, true, 30);

        // Displaying details
        System.out.println("---- Vehicle Details ----");
        mb.showDetails(); // From Vehicle class

        System.out.println("\n---- Bicycle Details ----");
        mb.showBicycleDetails(); // From Bicycle class

        System.out.println("\n---- MountainBike Details ----");
        mb.showMountainBikeDetails(); // From MountainBike class

        // Applying methods from different levels of inheritance
        mb.applyBrake(5); // From Vehicle class
        mb.adjustSeatHeight(35); // From MountainBike class
    }
}

class Vehicle {

    // Common fields for all vehicles
    public int speed;
    public int gear;

    // Constructor to initialise speed and gear
    public Vehicle(int spd, int gr) {
        speed = spd;
        gear = gr;
    }

    // Method to display vehicle details
    public void showDetails() {
        System.out.println("Speed: " + speed + " km/h");
        System.out.println("Gear: " + gear);
    }

    // Method to apply brakes
    public void applyBrake(int decrement) {
        speed -= decrement;
        System.out.println("Speed after applying brake: " + speed + " km/h");
    }
}

// Intermediate class (child of Vehicle)
class Bicycle extends Vehicle {

    // Additional field specific to Bicycle
    public boolean hasBell;

    // Constructor to initialise Bicycle properties
    public Bicycle(int spd, int gr, boolean bell) {
        super(spd, gr); // Calls the constructor of Vehicle
        hasBell = bell;
    }

    // Method to display Bicycle-specific details
    public void showBicycleDetails() {
        System.out.println("Has bell: " + (hasBell ? "Yes" : "No"));
    }
}

// Most specialised class (child of Bicycle)
class MountainBike extends Bicycle {

    // Additional field specific to MountainBike
    public int seatHeight;

    // Constructor to initialise MountainBike properties
    public MountainBike(int spd, int gr, boolean bell, int height) {
        super(spd, gr, bell); // Calls the constructor of Bicycle
        seatHeight = height;
    }

    // Method to adjust seat height
    public void adjustSeatHeight(int newHeight) {
        seatHeight = newHeight;
        System.out.println("Seat height adjusted to: " + seatHeight + " cm");
    }

    // Method to display MountainBike-specific details
    public void showMountainBikeDetails() {
        System.out.println("Seat Height: " + seatHeight + " cm");
    }
}

Output

Hierarchical Inheritance 

Hierarchical Inheritance in Java
Hierarchical Inheritance in Java

Understanding Hierarchical Inheritance through code

public class CompanyDemo {

    public static void main(String[] args) {

        Writer writer = new Writer();
        writer.name = "Alice";
        writer.id = 101;
        writer.work();            // Inherited from Employee
        writer.manageTeam();      // Specific to Writer

        System.out.println();

        Developer developer = new Developer();
        developer.name = "Bob";
        developer.id = 102;
        developer.work();         // Inherited from Employee
        developer.writeCode();    // Specific to Developer

        System.out.println();

        Designer designer = new Designer();
        designer.name = "Charlie";
        designer.id = 103;
        designer.work();          // Inherited from Employee
        designer.design();        // Specific to Designer
    }
}

class Employee {

    String name;
    int id;

    void work() {
        System.out.println(name + " is working.");
    }
}

class Writer extends Employee {

    void manageTeam() {
        System.out.println(name + " is managing the team.");
    }
}

class Developer extends Employee {

    void writeCode() {
        System.out.println(name + " is writing code.");
    }
}

class Designer extends Employee {

    void design() {
        System.out.println(name + " is designing.");
    }
}

Output

Multiple Inheritance in Java

Multiple inheritance in Java through Classes
Multiple Inheritance in Java through Classes
// Mom class
class Mom {

    public void cook() {
        System.out.println("Cooking delicious meals like Mom.");
    }
}

// Dad class
class Dad {

    public void paint() {
        System.out.println("Painting beautiful pictures like Dad.");
    }
}

// Trying to inherit from both Mom and Dad — NOT allowed
class Child extends Mom, Dad {

    public void showSkills() {
        cook();   // From Mom
        paint();  // From Dad
    }
}

// Main class
public class Main {

    public static void main(String[] args) {
        Child child = new Child();
        child.showSkills();
    }
}

Output

Multiple Inheritance in Java through Interface
Multiple Inheritance in Java through Interface
// Mom class
class Mom {

    public void cook() {
        System.out.println("Cooking delicious meals like Mom.");
    }
}

// Dad class
class Dad {

    public void paint() {
        System.out.println("Painting beautiful pictures like Dad.");
    }
}

// Trying to inherit from both Mom and Dad — NOT allowed
class Child extends Mom, Dad {

    public void showSkills() {
        cook();   // From Mom
        paint();  // From Dad
    }
}

// Main class
public class Main {

    public static void main(String[] args) {
        Child child = new Child();
        child.showSkills();
    }
}

Output

Why did this work?

Edge Case

interface InterfaceA {

    default void show() {
        System.out.println("InterfaceA's default show method");
    }
}

interface InterfaceB {

    default void show() {
        System.out.println("InterfaceB's default show method");
    }
}

class MyClass implements InterfaceA, InterfaceB {

    // No method override here — this will cause a compile-time error
}

public class Main {

    public static void main(String[] args) {
        MyClass obj = new MyClass();
        obj.show(); // ❌ Compile-time error: Duplicate default methods
    }
}

Output

By Overriding
interface InterfaceA {

    default void show() {
        System.out.println("InterfaceA's default show method");
    }
}

interface InterfaceB {

    default void show() {
        System.out.println("InterfaceB's default show method");
    }
}

class MyClass implements InterfaceA, InterfaceB {

    @Override
    public void show() {
        System.out.println("MyClass's version of the show method");
    }
}

public class Main {

    public static void main(String[] args) {
        MyClass obj = new MyClass();
        obj.show();
    }
}
Output
Prioritising one over the other
interface InterfaceA {

    default void show() {
        System.out.println("InterfaceA's default show method");
    }
}

interface InterfaceB {

    default void show() {
        System.out.println("InterfaceB's default show method");
    }
}

class MyClass implements InterfaceA, InterfaceB {

    @Override
    public void show() {
        InterfaceA.super.show(); // Calls InterfaceA's default method
    }
}

public class Main {

    public static void main(String[] args) {
        MyClass obj = new MyClass();
        obj.show();
    }
}
Output

Multiple inheritance in classes using Composition

public class Main {

    public static void main(String[] args) {
        Child child = new Child();
        child.showSkills();
    }
}

// Mom class
class Mom {

    public void cook() {
        System.out.println("Cooking like Mom.");
    }
}

// Dad class
class Dad {

    public void paint() {
        System.out.println("Painting like Dad.");
    }
}

// Child class has-a Mom and Dad
class Child {

    private Mom mom;
    private Dad dad;

    public Child() {
        mom = new Mom();
        dad = new Dad();
    }

    public void showSkills() {
        mom.cook();   // Delegating to Mom
        dad.paint();  // Delegating to Dad
    }
}

Output

Hybrid Inheritance 

Hybrid Inheritance without Multiple Inheritance

Hybrid Inheritance without Multiple Inheritance in Java
Hybrid Inheritance without Multiple Inheritance in Java
public class FamilyTree {

    public static void main(String[] args) {

        Son son = new Son();

        // Inherited from Grandfather
        son.showGrandfather();

        // Inherited from Father
        son.showFather();

        // Own method
        son.showSon();

        // Line break
        System.out.print("\n");

        // Demonstrating Uncle separately
        Uncle uncle = new Uncle();
        uncle.showGrandfather(); // Inherited from Grandfather
        uncle.showUncle();       // Uncle's own method
    }
}

// Base class
class Grandfather {

    void showGrandfather() {
        System.out.println("I am the Grandfather.");
    }
}

// Father class inherits from Grandfather
class Father extends Grandfather {

    void showFather() {
        System.out.println("I am the Father.");
    }
}

// Uncle class also inherits from Grandfather (hierarchical inheritance)
class Uncle extends Grandfather {

    void showUncle() {
        System.out.println("I am the Uncle.");
    }
}

// Son class inherits from Father (Single Inheritance)
class Son extends Father {

    void showSon() {
        System.out.println("I am the Son.");
    }
}

Output

Hybrid Inheritance using Multiple Inheritance

public class FamilyTree {

    public static void main(String[] args) {

        Son son = new Son();

        // Inherited from Grandfather
        son.showGrandfather();

        // Inherited from Father
        son.showFather();

        // Inherited from Uncle interface
        son.showUncle();

        // Son's own method
        son.showSon();

        // Line break
        System.out.print("\n");

        // Demonstrating actual Uncle implementation
        UncleImpl uncle = new UncleImpl();
        uncle.showGrandfather(); // Inherited from Grandfather
        uncle.showUncle();       // Inherited from Uncle interface
        uncle.old();
    }
}

// Base class
class Grandfather {

    void showGrandfather() {
        System.out.println("I am the Grandfather.");
    }
}

// Father class inherits from Grandfather (single inheritance)
class Father extends Grandfather {

    void showFather() {
        System.out.println("I am the Father.");
    }
}

// Declaring Uncle as an interface (not a class)
interface Uncle {

    default void showUncle() {
        System.out.println("I am the Uncle.");
    }
}

// Son inherits from Father and implements Uncle (multiple inheritance via interface)
class Son extends Father implements Uncle {

    void showSon() {
        System.out.println("I am the Son.");
    }
}

// Concrete class to represent Uncle independently
class UncleImpl extends Grandfather implements Uncle {

    void old() {
        System.out.print("Hi! I am UncleImpl");
    }
}

Output

Hybrid Inheritance with Multiple Inheritance in Java
Hybrid Inheritance with Multiple Inheritance in Java

Advantages of inheritance in Java

Disadvantages of Inheritance in Java

Important Points 

Conclusion

Frequently Asked Questions

Q1. What is the use of the ‘extends’ keyword?
Q2. What is the difference between multiple and multilevel Inheritance in Java?
Q3. Why is Inheritance used?
Q4. Can a child class inherit private members of the parent class?
Q5. Can a final class be inherited in Java?
Q6. What is the role of constructors in inheritance?
Share your love