---
title: "Basics of Inheritance"  
description: "Inheritance is a process in which properties of parent class are inherited by the child class."  
author: "NAMAN PANDEY"  
published: 2018-06-19  
updated: 2019-09-07  
canonical: https://www.mindstick.com/articles/12955/basics-of-inheritance  
category: "c#"  
tags: ["inheritance"]  
reading_time: 4 minutes  

---

# Basics of Inheritance

**INHERITANCE:** Inheritance is a process in which [properties](https://www.mindstick.com/blog/673/properties) of parent class are inherited by the [child class](https://www.mindstick.com/forum/159389/how-to-remove-child-class-from-active-div).For example if there is a parent class and a child class and we declare a [variable](https://answers.mindstick.com/qa/49024/what-year-was-variable-wing-invented-and-how) or method in the parent class we can use that in child class without declaring that variable or method again.There following are the types of inheritance:**1) SINGLE INHERITANCE** ![Basics of Inheritance](https://www.mindstick.com/mindstickarticle/0c8f4e5d-2b10-4962-99d5-0a60e2da73a9/images/d8f033dc-cd36-4dd0-82f0-41c27d8cb1e7.png)**\**Above diagram shows the concept of single inheritance.In single inheritance base class derive the properties from only one parent class.**Syntax:**

```
class subclass_name : access_mode base_class{  //body of subclass};
```

## EXAMPLE:

```
// C++ program to explain // Single inheritance#include <iostream>using namespace std;
// base classclass Vehicle {  public:    Vehicle()    {      cout << "This is a Vehicle" << endl;    }};

// sub class derived from two base classesclass Car: public Vehicle{
};
// main functionint main(){     // creating object of sub class will    // invoke the constructor of base classes    Car obj;    return 0;}
Output:This is a vehicle
```

## 2) MULTIPLE INHERITANCE:

![Basics of Inheritance](https://www.mindstick.com/mindstickarticle/0c8f4e5d-2b10-4962-99d5-0a60e2da73a9/images/452525a0-b1bc-4b79-9037-8c57ffdd0b27.jpg)**\**

In [multiple inheritance](https://www.mindstick.com/forum/2519/java-multiple-inheritance) properties of sub/chlid class are derived from from two super/parent classes.

## EXAMPLE:

```
// C++ program to explain // multiple inheritance#include <iostream>using namespace std;// first base classclass Vehicle {  public:    Vehicle()    {      cout << "This is a Vehicle" << endl;    }};
// second base classclass FourWheeler {  public:    FourWheeler()    {      cout << "This is a 4 wheeler Vehicle" << endl;    }};
// sub class derived from two base classesclass Car: public Vehicle, public FourWheeler {
};
// main functionint main(){     // creating object of sub class will    // invoke the constructor of base classes    Car obj;    return 0;}
Output:This is a VehicleThis is a 4 wheeler Vehicle
```

**3) [MULTILEVEL INHERITANCE](https://www.mindstick.com/interview/23096/can-you-explain-me-multilevel-inheritance):**

![Basics of Inheritance](https://www.mindstick.com/mindstickarticle/0c8f4e5d-2b10-4962-99d5-0a60e2da73a9/images/080f94ad-b33b-404c-80be-828172be8626.png)**\**

In multilevel inheritance,a derived class is derived from another base class.

## EXAMPLE:

```
// C++ program to implement // Multilevel Inheritance
#include <iostream>using namespace std;
// base classclass Vehicle {  public:    Vehicle()    {      cout << "This is a Vehicle" << endl;    }};
class fourWheeler: public Vehicle{ public:    fourWheeler()    {      cout<<"Objects with 4 wheels are vehicles"<<endl;    }
};
// sub class derived from two base classesclass Car: public fourWheeler{   public:     car()     {       cout<<"Car has 4 Wheels"<<endl;     }};
// main functionint main(){     //creating object of sub class will    //invoke the constructor of base classes    Car obj;    return 0;}
output:This is a VehicleObjects with 4 wheels are vehiclesCar has 4 Wheels
```

## 4) HIERARCHICAL INHERITANCE:

![Basics of Inheritance](https://www.mindstick.com/mindstickarticle/0c8f4e5d-2b10-4962-99d5-0a60e2da73a9/images/a49ab294-571e-4a99-85ca-b89f3776b3c6.png)**\**

In hierarchical inheritance,more than one [sub class](https://answers.mindstick.com/qa/92538/what-are-the-base-class-sub-class-and-superclass) is inherited from a single [base class](https://www.mindstick.com/interview/2426/what-is-the-base-class-for-error-and-exception). i.e. more than one [derived class](https://answers.mindstick.com/qa/30686/what-is-a-base-class-and-derived-class) is created from a single base class.

## EXAMPLE:

```
// C++ program to implement // Hierarchical Inheritance#include <iostream>using namespace std; // base classclass Vehicle {  public:    Vehicle()    {      cout << "This is a Vehicle" << endl;    }};
// first sub class class Car: public Vehicle
{
};
// second sub classclass Bus: public Vehicle{
};
// main functionint main(){     // creating object of sub class will    // invoke the constructor of base class    Car obj1;    Bus obj2;    return 0;}
Output:This is a VehicleThis is a Vehicle
```

## 5) HYBRID INHERITANCE:

![Basics of Inheritance](https://www.mindstick.com/mindstickarticle/0c8f4e5d-2b10-4962-99d5-0a60e2da73a9/images/4f9f5713-a593-49d4-bdd3-4b66d5270b53.png)**\**

Hybrid Inheritance is implemented by combining more than one type of inheritance. For example: Combining Hierarchical inheritance and Multiple Inheritance.

## EXAMPLE:

```
// C++ program for Hybrid Inheritance
#include <iostream>using namespace std;
// base class class Vehicle
{  public:
    Vehicle()    {      cout << "This is a Vehicle" << endl;
    }
};
//base classclass Fare{    public:    Fare()    {        cout<<"Fare of Vehicle\n";    }};
// first sub class class Car: public Vehicle{
};
// second sub classclass Bus: public Vehicle, public Fare{
};
// main functionint main(){     // creating object of sub class will    // invoke the constructor of base class    Bus obj2;    return 0;}
Output:This is a VehicleFare of Vehicle
```

## TYPES OF INHERITANCE SUPPORTED BY JAVA:

Three types of inheritance are supported by java which are:

1)Single Inheritance

2)Multilevel Inheritance

3)Hierarchical Inheritance

There are two more types of inheritance supported by java but through **interface** only:

1)Multiple Inheritance

2)Hybrid Inheritance

The reason that Java does not support multiple inheritances is that of the problem known as **"diamond problem"**.

---

Original Source: https://www.mindstick.com/articles/12955/basics-of-inheritance

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
