Login

Sign Up

Inheritance
Tushar Varshney

Posted on May 30, 2025 | Coding

Inheritance

Inheritance Today, we're exploring one of the core pillars of Object-Oriented Programming — Inheritance. If you've ever wished you could write less code while doing more, inheritance is your new best friend. It allows you to create new classes that reuse, extend, or modify the behavior of existing ones. Think of it like passing traits from parent to child — just like a child might inherit eye color or height, classes in OOP can inherit properties and behaviors from other classes.


List of Content :

  • What is Inheritance?
  • Types of Inheritance
  • How inheritance works in popular languages (Python/Java/C++)
  • Using super() to access parent class functionality
  • How inheritance supports polymorphism
  • Test your understanding with a short quiz or coding task

What is Inheritance? :

Inheritance is a fundamental concept in Object-Oriented Programming (OOP) that allows one class (called the child or subclass) to inherit the properties and behaviors (methods and attributes) of another class (called the parent or superclass).

In simpler terms, it’s like a family tree in code. Just as a child inherits characteristics from their parents, a subclass inherits features from its superclass — and can even add its own unique traits or modify the inherited ones.

Inheritance image

Types of Inheritance:

Let’s explore the 5 major types of inheritance supported conceptually in OOP, with Java code examples to guide you. 💡

1️⃣ Single Inheritance

👉 One subclass inherits from one superclass.

class Animal {
    void speak() {
        System.out.println("Animal makes a sound");
    }
}

class Dog extends Animal {
    void bark() {
        System.out.println("Dog barks");
    }
}

public class Main {
    public static void main(String[] args) {
        Dog d = new Dog();
        d.speak();  // Inherited
        d.bark();   // Own method
    }
}


2️⃣ Multiple Inheritance (Using Interfaces)

👉 A class implements multiple interfaces.

📌 Example: FlyingCar inherits behavior from Car and Airplane interfaces.

interface Car {
    void drive();
}

interface Airplane {
    void fly();
}

class FlyingCar implements Car, Airplane {
    public void drive() {
        System.out.println("Driving on road");
    }

    public void fly() {
        System.out.println("Flying in sky");
    }
}

public class Main {
    public static void main(String[] args) {
        FlyingCar fc = new FlyingCar();
        fc.drive();
        fc.fly();
    }
}


3️⃣ Multilevel Inheritance

👉 A class inherits from a subclass that itself inherits from a superclass.

📌 Example: Grandparent → Parent → Child

class Grandparent {
    void legacy() {
        System.out.println("Legacy from grandparent");
    }
}

class Parent extends Grandparent {
    void wisdom() {
        System.out.println("Wisdom from parent");
    }
}

class Child extends Parent {
    void dream() {
        System.out.println("Child’s dream");
    }
}

public class Main {
    public static void main(String[] args) {
        Child c = new Child();
        c.legacy();
        c.wisdom();
        c.dream();
    }
}


4️⃣ Hierarchical Inheritance

👉 Multiple classes inherit from the same parent class.

📌 Example: Dog and Cat inherit from Animal.

class Animal {
    void breathe() {
        System.out.println("Breathing...");
    }
}

class Dog extends Animal {
    void bark() {
        System.out.println("Bark!");
    }
}

class Cat extends Animal {
    void meow() {
        System.out.println("Meow!");
    }
}

public class Main {
    public static void main(String[] args) {
        Dog d = new Dog();
        Cat c = new Cat();
        d.breathe();
        d.bark();
        c.breathe();
        c.meow();
    }
}

5️⃣ Hybrid Inheritance (Using Interfaces)

👉 A mix of different inheritance types — supported in Java only through interfaces.

interface A {
    void methodA();
}

interface B extends A {
    void methodB();
}

interface C {
    void methodC();
}

class D implements B, C {
    public void methodA() {
        System.out.println("A");
    }

    public void methodB() {
        System.out.println("B");
    }

    public void methodC() {
        System.out.println("C");
    }

    void methodD() {
        System.out.println("D");
    }
}

public class Main {
    public static void main(String[] args) {
        D obj = new D();
        obj.methodA();
        obj.methodB();
        obj.methodC();
        obj.methodD();
    }
}

📊 Inheritance Support Across Languages

Inheritance Type Java Python C++
Single Inheritance ✅ Fully supported ✅ Fully supported ✅ Fully supported
Multiple Inheritance ❌ Not supported with classes; only via interfaces ✅ Fully supported ✅ Fully supported
Multilevel Inheritance ✅ Fully supported ✅ Fully supported ✅ Fully supported
Hierarchical Inheritance ✅ Fully supported ✅ Fully supported ✅ Fully supported
Hybrid Inheritance ✅ Supported via interfaces ✅ Fully supported ✅ Fully supported


🔹 Using super() to Access Parent Class Functionality 🛠️

In inheritance, sometimes you want a child class to override a method from its parent class but still be able to call the original version of that method. This is where the super keyword in Java comes in handy! ✨

What is super?

  • super refers to the immediate parent class of the current class.

  • You can use super to:

    • Call a parent class method that’s overridden in the child.

    • Access the parent class constructor.

    • Access parent class variables (if needed).

Example: Calling Parent Method with super :

class Animal {
    void speak() {
        System.out.println("Animal makes a sound");
    }
}

class Dog extends Animal {
    @Override
    void speak() {
        super.speak();  // Calls Animal's speak()
        System.out.println("Dog barks");
    }
}

public class Main {
    public static void main(String[] args) {
        Dog d = new Dog();
        d.speak();
    }
}

Output :

Animal makes a sound
Dog barks

Here, Dog overrides speak() but still calls the original speak() of Animal via super.speak().

Example: Using super() in Constructors:

class Vehicle {
    Vehicle() {
        System.out.println("Vehicle constructor called");
    }
}

class Car extends Vehicle {
    Car() {
        super();  // Calls Vehicle constructor
        System.out.println("Car constructor called");
    }
}

public class Main {
    public static void main(String[] args) {
        Car c = new Car();
    }
}

Output :

Vehicle constructor called
Car constructor called

Using super() in the constructor ensures the parent class is properly initialized first.


🔹 How Inheritance Supports Polymorphism 🤝✨

Inheritance and polymorphism go hand in hand in Object-Oriented Programming (OOP). Without inheritance, polymorphism wouldn’t be possible!

How does inheritance enable polymorphism?

Because a subclass “is a” type of its superclass, you can:

  • Use a parent class reference to point to any of its child class objects.

  • Call overridden methods on these objects, and the actual method that runs depends on the object type (not the reference type). This is called dynamic method dispatch or runtime polymorphism.

Example :

class Animal {
    void sound() {
        System.out.println("Animal makes a sound");
    }
}

class Dog extends Animal {
    @Override
    void sound() {
        System.out.println("Dog barks");
    }
}

class Cat extends Animal {
    @Override
    void sound() {
        System.out.println("Cat meows");
    }
}

public class Main {
    public static void main(String[] args) {
        Animal myAnimal;

        myAnimal = new Dog();
        myAnimal.sound();  // Outputs: Dog barks

        myAnimal = new Cat();
        myAnimal.sound();  // Outputs: Cat meows
    }
}

Here, the variable myAnimal of type Animal can hold objects of both Dog and Cat. The correct overridden sound() method is called at runtime depending on the actual object.


🧠 Test Your Understanding: Quiz 💻

Let’s see how well you’ve grasped the concepts of Inheritance and Polymorphism so far! Ready? Let’s go! 🚀

  1. Which keyword is used in Java to inherit a class?
    A) implements
    B) extends
    C) inherits
    D) super

  2. What type of inheritance does this represent?

class A {}
class B extends A {}
class C extends B {}

A) Single inheritance
B) Multiple inheritance
C) Multilevel inheritance
D) Hierarchical inheritance

  1. What does the super keyword do?
    A) Calls child class constructor
    B) Calls parent class constructor or methods
    C) Declares a new method
    D) None of the above

  2. What is polymorphism in OOP?
    A) One class inherits multiple classes
    B) A function can have multiple names
    C) Objects of different classes can be treated as objects of a common superclass
    D) None of the above


Happy Coding!!

7 Reactions

0 Bookmarks

Read next

Tushar Varshney

Tushar Varshney

Jan 6, 25

5 min read

|

Problem on Bubble Sort

Tushar Varshney

Tushar Varshney

Jan 8, 25

6 min read

|

Problem on Selection sort