Inheritance

Last Updated : 9 Oct, 2026

Inheritance is an important concept in Object-Oriented Programming (OOP) that allows a new class to acquire properties and behaviors from an existing class. It helps developers reuse existing code and create relationships between classes.

  • The existing class is called the Parent Class (Base Class or Superclass).
  • The new class that inherits from the parent class is called the Child Class (Derived Class or Subclass).
  • Inheritance supports code reusability, extensibility, and hierarchical class organization.
C++
#include <iostream>
using namespace std;

// Parent class
class Vehicle {
public:
    void start() {
        cout << "Vehicle is starting" << endl;
    }
};

// Child classes
class Car : public Vehicle {
};

class Bus : public Vehicle {
};

class Truck : public Vehicle {
};

int main() {
    Car car;
    Bus bus;
    Truck truck;

    car.start();
    bus.start();
    truck.start();

    return 0;
}
Java
/* Parent class */
class Vehicle {
    public void start() {
        System.out.println("Vehicle is starting");
    }
}

/* Child classes */
class Car extends Vehicle {
}

class Bus extends Vehicle {
}

class Truck extends Vehicle {
}

public class Main {
    public static void main(String[] args) {
        Car car = new Car();
        Bus bus = new Bus();
        Truck truck = new Truck();

        car.start();
        bus.start();
        truck.start();
    }
}
Python
# Parent class
class Vehicle:
    def start(self):
        print("Vehicle is starting")

# Child classes
class Car(Vehicle):
    pass

class Bus(Vehicle):
    pass

class Truck(Vehicle):
    pass

if __name__ == '__main__':
    car = Car()
    bus = Bus()
    truck = Truck()

    car.start()
    bus.start()
    truck.start()
C#
// Parent class
public class Vehicle
{
    public void Start()
    {
        System.Console.WriteLine("Vehicle is starting");
    }
}

// Child classes
public class Car : Vehicle
{
}

public class Bus : Vehicle
{
}

public class Truck : Vehicle
{
}

public class Program
{
    public static void Main(string[] args)
    {
        Car car = new Car();
        Bus bus = new Bus();
        Truck truck = new Truck();

        car.Start();
        bus.Start();
        truck.Start();
    }
}
JavaScript
// Parent class
class Vehicle {
    start() {
        console.log('Vehicle is starting');
    }
}

// Child classes
class Car extends Vehicle {
}

class Bus extends Vehicle {
}

class Truck extends Vehicle {
}

const car = new Car();
const bus = new Bus();
const truck = new Truck();

car.start();
bus.start();
truck.start();

Output
Vehicle is starting
Vehicle is starting
Vehicle is starting

Explanation:

  • Vehicle: Parent class that defines the start() method.
  • Car, Bus, and Truck: Child classes that publicly inherit from Vehicle.
  • Code Reusability: All three child classes can call the inherited start() method without defining it separately.

How Inheritance Works

Inheritance establishes a relationship between a parent class and a child class. The child class can reuse accessible members of the parent class and define additional features of its own.

Inheritance

In this example, Vehicle is the parent class, while Car, Bus, and Truck are child classes.

  • Parent Class: Defines common features such as fuelAmount(), capacity(), and applyBrakes().
  • Child Class: Inherits accessible features from the parent class and can define its own features.
  • Code Reusability: Shared functionality is implemented in the parent class instead of being repeated in every child class.

Types of Inheritance in Programming 

Inheritance can be classified into different types based on how classes are related.

1. Single Inheritance

Single inheritance occurs when one child class inherits from one parent class. It creates a direct relationship between two classes.

inheritence

Example: A Car class inherits common properties and methods from an Vehicle class.

C++
//Super class
class Vehicle {
public:
    Vehicle() {
        std::cout << "This is a Vehicle" << std::endl;
    }
};

// Subclass 
class Car : public Vehicle {
public:
    Car() {
        std::cout << "This Vehicle is Car" << std::endl;
    }
};

int main() {
    // Creating object of subclass invokes base class constructor
    Car obj;
    return 0;
}
Java
//Super class
class Vehicle {
    Vehicle() {
        System.out.println("This is a Vehicle");
    }
}

// Subclass 
class Car extends Vehicle {
    Car() {
        System.out.println("This Vehicle is Car");
    }
}

public class Test {
    public static void main(String[] args) {
        // Creating object of subclass invokes base class constructor
        Car obj = new Car();
    }
}
Python
#Super class
class Vehicle:
    def __init__(self):
        print("This is a Vehicle")

# Subclass 
class Car(Vehicle):
    def __init__(self):
        super().__init__()
        print("This Vehicle is Car")

# Creating object of subclass invokes base class constructor
obj = Car()
C#
//Super class
class Vehicle {
    public Vehicle() {
        System.Console.WriteLine("This is a Vehicle");
    }
}

// Subclass 
class Car : Vehicle {
    public Car() : base() {
        System.Console.WriteLine("This Vehicle is Car");
    }
}

public class Test {
    public static void Main(string[] args) {
        // Creating object of subclass invokes base class constructor
        Car obj = new Car();
    }
}
JavaScript
//Super class
class Vehicle {
    constructor() {
        console.log("This is a Vehicle");
    }
}

// Subclass 
class Car extends Vehicle {
    constructor() {
        super();
        console.log("This Vehicle is Car");
    }
}

// Creating object of subclass invokes base class constructor
let obj = new Car();

Output
This is a Vehicle
This Vehicle is Car

2. Multilevel Inheritance

Multilevel inheritance occurs when a class inherits from another derived class. It creates a chain of inheritance across multiple levels.

inheritence2

Father inherits from Grandfather, and Child inherits from Father. The Child class can reuse accessible functionality from both ancestor classes.

C++
#include <iostream>
using namespace std;

class Vehicle {
public:
    Vehicle() {
        cout << "This is a Vehicle" << endl;
    }
};

class FourWheeler : public Vehicle {
public:
    FourWheeler() {
        cout << "4 Wheeler Vehicles" << endl;
    }
};

class Car : public FourWheeler {
public:
    Car() {
        cout << "This 4 Wheeler Vehicle is a Car" << endl;
    }
};

int main() {
    Car obj; // Triggers all constructors in order
    return 0;
}
Java
class Vehicle {
    Vehicle() {
        System.out.println("This is a Vehicle");
    }
}
class FourWheeler extends Vehicle {
    FourWheeler() {
        System.out.println("4 Wheeler Vehicles");
    }
}
class Car extends FourWheeler {
    Car() {
        System.out.println("This 4 Wheeler Vehicle is a Car");
    }
}
public class Geeks {
    public static void main(String[] args) {
        Car obj = new Car(); // Triggers all constructors in order
    }
}
Python
class Vehicle:
    def __init__(self):
        print("This is a Vehicle")

class FourWheeler(Vehicle):
    def __init__(self):
        super().__init__()
        print("4 Wheeler Vehicles")

class Car(FourWheeler):
    def __init__(self):
        super().__init__()
        print("This 4 Wheeler Vehicle is a Car")

if __name__ == "__main__":
    obj = Car() # Triggers all constructors in order
C#
using System;

class Vehicle {
    public Vehicle() {
        Console.WriteLine("This is a Vehicle");
    }
}

class FourWheeler : Vehicle {
    public FourWheeler() : base() {
        Console.WriteLine("4 Wheeler Vehicles");
    }
}

class Car : FourWheeler {
    public Car() : base() {
        Console.WriteLine("This 4 Wheeler Vehicle is a Car");
    }
}

class Geeks {
    public static void Main(String[] args) {
        Car obj = new Car(); // Triggers all constructors in order
    }
}
JavaScript
class Vehicle {
    constructor() {
        console.log('This is a Vehicle');
    }
}

class FourWheeler extends Vehicle {
    constructor() {
        super();
        console.log('4 Wheeler Vehicles');
    }
}

class Car extends FourWheeler {
    constructor() {
        super();
        console.log('This 4 Wheeler Vehicle is a Car');
    }
}

let obj = new Car(); // Triggers all constructors in order

Output
This is a Vehicle
4 Wheeler Vehicles
This 4 Wheeler Vehicle is a Car

3. Multiple Inheritance

Multiple inheritance occurs when one child class inherits from more than one parent class. It allows the child class to combine features from multiple classes.

Multiple_inheritance
C++
#include <iostream>
using namespace std;

class LandVehicle {
public:
    virtual void landInfo() {
        cout << "This is a LandVehicle" << endl;
    }
};

class WaterVehicle {
public:
    virtual void waterInfo() {
        cout << "This is a WaterVehicle" << endl;
    }
};

class AmphibiousVehicle : public LandVehicle, public WaterVehicle {
public:
    AmphibiousVehicle() {
        cout << "This is an AmphibiousVehicle" << endl;
    }
};

int main() {
    AmphibiousVehicle obj;
    obj.waterInfo();
    obj.landInfo();
    return 0;
}
Java
interface LandVehicle {
    default void landInfo() {
        System.out.println("This is a LandVehicle");
    }
}
interface WaterVehicle {
    default void waterInfo() {
        System.out.println("This is a WaterVehicle");
    }
}
// Subclass implementing both interfaces
class AmphibiousVehicle implements LandVehicle, WaterVehicle {
    AmphibiousVehicle() {
        System.out.println("This is an AmphibiousVehicle");
    }
}
public class Test {
    public static void main(String[] args) {
        AmphibiousVehicle obj = new AmphibiousVehicle();
        obj.waterInfo();
        obj.landInfo();
    }
}
Python
class LandVehicle:
    def landInfo(self):
        print("This is a LandVehicle")

class WaterVehicle:
    def waterInfo(self):
        print("This is a WaterVehicle")

class AmphibiousVehicle(LandVehicle, WaterVehicle):
    def __init__(self):
        print("This is an AmphibiousVehicle")
        LandVehicle.__init__(self)
        WaterVehicle.__init__(self)

if __name__ == "__main__":
    obj = AmphibiousVehicle()
    obj.waterInfo()
    obj.landInfo()
C#
using System;

// Interface LandVehicle
interface LandVehicle {
    void landInfo();
}

// Interface WaterVehicle
interface WaterVehicle {
    void waterInfo();
}

// Subclass implementing both interfaces
class AmphibiousVehicle : LandVehicle, WaterVehicle {
    public AmphibiousVehicle() {
        Console.WriteLine("This is an AmphibiousVehicle");
    }

    public void landInfo() {
        Console.WriteLine("This is a LandVehicle");
    }

    public void waterInfo() {
        Console.WriteLine("This is a WaterVehicle");
    }
}

public class Test {
    public static void Main(string[] args) {
        AmphibiousVehicle obj = new AmphibiousVehicle();
        obj.waterInfo();
        obj.landInfo();
    }
}
JavaScript
const LandVehicle = {
    landInfo() {
        console.log("This is a LandVehicle");
    }
};

const WaterVehicle = {
    waterInfo() {
        console.log("This is a WaterVehicle");
    }
};

class AmphibiousVehicle {
    constructor() {
        console.log("This is an AmphibiousVehicle");
        Object.assign(this, LandVehicle);
        Object.assign(this, WaterVehicle);
    }
}

const obj = new AmphibiousVehicle();
obj.waterInfo();
obj.landInfo();

Output
This is an AmphibiousVehicle
This is a WaterVehicle
This is a LandVehicle

4. Hierarchical Inheritance

Hierarchical inheritance occurs when multiple child classes inherit from the same parent class. It allows related classes to share common functionality.

hierarchical_inheritance
C++
#include <iostream>
using namespace std;

class Vehicle
{
  public:
    Vehicle()
    {
        cout << "This is a Vehicle" << endl;
    }
};

class Car : public Vehicle
{
  public:
    Car()
    {
        cout << "This Vehicle is Car" << endl;
    }
};

class Bus : public Vehicle
{
  public:
    Bus()
    {
        cout << "This Vehicle is Bus" << endl;
    }
};

int main()
{
    Car obj1;
    Bus obj2;
    return 0;
}
Java
import java.util.ArrayList;

class Vehicle {
  public Vehicle() {
    System.out.println("This is a Vehicle");
  }
}

class Car extends Vehicle {
  public Car() {
    System.out.println("This Vehicle is Car");
  }
}

class Bus extends Vehicle {
  public Bus() {
    System.out.println("This Vehicle is Bus");
  }
}

public class Main {
  public static void main(String[] args) {
    Car obj1 = new Car();
    Bus obj2 = new Bus();
  }
}
Python
class Vehicle:
  def __init__(self):
    print("This is a Vehicle")

class Car(Vehicle):
  def __init__(self):
    super().__init__()
    print("This Vehicle is Car")

class Bus(Vehicle):
  def __init__(self):
    super().__init__()
    print("This Vehicle is Bus")

if __name__ == '__main__':
  obj1 = Car()
  obj2 = Bus()
C#
using System;

class Vehicle
{
  public Vehicle()
  {
    Console.WriteLine("This is a Vehicle");
  }
}

class Car : Vehicle
{
  public Car() : base()
  {
    Console.WriteLine("This Vehicle is Car");
  }
}

class Bus : Vehicle
{
  public Bus() : base()
  {
    Console.WriteLine("This Vehicle is Bus");
  }
}

class Program
{
  static void Main(string[] args)
  {
    Car obj1 = new Car();
    Bus obj2 = new Bus();
  }
}
JavaScript
class Vehicle {
  constructor() {
    console.log('This is a Vehicle');
  }
}

class Car extends Vehicle {
  constructor() {
    super();
    console.log('This Vehicle is Car');
  }
}

class Bus extends Vehicle {
  constructor() {
    super();
    console.log('This Vehicle is Bus');
  }
}

const obj1 = new Car();
const obj2 = new Bus();

Output
This is a Vehicle
This Vehicle is Car
This is a Vehicle
This Vehicle is Bus

5. Hybrid Inheritance

In hybrid inheritence when two or more types of inheritance are combined in one program. For example, a class might use multiple inheritance and also be part of a multilevel inheritance chain.

inheritence_5
C++
#include <iostream>
using namespace std;

class Vehicle
{
  public:
    Vehicle()
    {
        cout << "This is a Vehicle" << endl;
    }
};

class Fare
{
  public:
    Fare()
    {
        cout << "Fare of Vehicle" << endl;
    }
};

class Car : public Vehicle
{
  public:
    Car()
    {
        cout << "This Vehicle is a Car" << endl;
    }
};

class Bus : public Vehicle, public Fare
{
  public:
    Bus()
    {
        cout << "This Vehicle is a Bus with Fare";
    }
};

int main()
{
    Bus obj2;
    return 0;
}
Java
public class Vehicle {
  {
    System.out.println("This is a Vehicle");
  }
}

class Fare {
  {
    System.out.println("Fare of Vehicle");
  }
}

class Car extends Vehicle {
  {
    System.out.println("This Vehicle is a Car");
  }
}

class Bus extends Vehicle {
  Fare fare = new Fare();
  {
    System.out.println("This Vehicle is a Bus with Fare");
  }
}

public class Main {
  public static void main(String[] args) {
    Bus obj2 = new Bus();
  }
}
Python
class Vehicle:
    def __init__(self):
        print("This is a Vehicle")

class Fare:
    def __init__(self):
        print("Fare of Vehicle")

class Car(Vehicle):
    def __init__(self):
        super().__init__()
        print("This Vehicle is a Car")

class Bus(Vehicle):
    def __init__(self):
        super().__init__()
        self.fare = Fare()
        print("This Vehicle is a Bus with Fare")

if __name__ == '__main__':
    obj2 = Bus()
C#
using System;

public class Vehicle
{
  public Vehicle()
  {
    Console.WriteLine("This is a Vehicle");
  }
}

public class Fare
{
  public Fare()
  {
    Console.WriteLine("Fare of Vehicle");
  }
}

public class Car : Vehicle
{
  public Car() : base()
  {
    Console.WriteLine("This Vehicle is a Car");
  }
}

public class Bus : Vehicle
{
  Fare fare = new Fare();
  public Bus() : base()
  {
    Console.WriteLine("This Vehicle is a Bus with Fare");
  }
}

public class Program
{
  public static void Main(string[] args)
  {
    Bus obj2 = new Bus();
  }
}
JavaScript
class Vehicle {
  constructor() {
    console.log('This is a Vehicle');
  }
}

class Fare {
  constructor() {
    console.log('Fare of Vehicle');
  }
}

class Car extends Vehicle {
  constructor() {
    super();
    console.log('This Vehicle is a Car');
  }
}

class Bus extends Vehicle {
  constructor() {
    super();
    this.fare = new Fare();
    console.log('This Vehicle is a Bus with Fare');
  }
}

const obj2 = new Bus();

Output
This is a Vehicle
Fare of Vehicle
This Vehicle is a Bus with Fare

Important Concepts of Inheritance

Several OOP concepts help explain how inheritance works across programming languages.

1. Method Overriding

Method overriding allows a child class to provide its own implementation of an inherited method, where the language permits overriding.

Example: A Vehicle class defines a start() method, while Car provides a specialized implementation of start().

2. Access Modifiers

Access modifiers control which class members can be accessed from a child class.

  • Public: Generally accessible from other classes.
  • Protected: Accessible within the class and its subclasses, subject to language rules.
  • Private: Not directly accessible from subclasses.

3. Constructor Execution

When a child-class object is created, the relevant parent-class constructor is generally executed as part of initialization, according to the language's constructor rules.

4. Abstract Classes

An abstract class can define common functionality and require child classes to provide implementations for specified operations.

Advantages of Inheritance

  • Code Reusability: Shared functionality can be reused across related classes.
  • Reduced Duplication: Common methods and properties do not need to be implemented repeatedly.
  • Extensibility: Child classes can introduce new behavior without modifying the parent class.
  • Polymorphism: Child objects can often be used through parent-class references, enabling flexible designs.
  • Logical Organization: Related classes can be arranged in a clear hierarchy.

Limitations of Inheritance

  • Tight Coupling: Changes to a parent class may affect its child classes.
  • Increased Complexity: Deep inheritance hierarchies can be difficult to understand and maintain.
  • Ambiguity: Multiple inheritance may cause conflicts when parent classes define similar members.
  • Limited Flexibility: Inheritance relationships are typically established through class definitions rather than changed dynamically.

Applications of Inheritance

Inheritance is commonly used when related entities share functionality but also require specialized behavior.

  • Vehicle Management Systems: Car, Bus, and Truck can inherit common features from Vehicle.
  • Employee Management Systems: Manager and Developer can inherit shared employee details from Employee.
  • GUI Frameworks: Specialized UI controls can extend common component classes.
  • Game Development: Different character types can inherit shared movement or health-related behavior.
  • Exception Handling: Specialized exception classes can inherit from a common exception base class.
Comment