Unveiling the power of Inheritance in Object Oriented Programming
Tags: oop, csharp, dotnet, programming
Published: 2023-12-20 18:42:55

**Inheritance** is a fundamental concept in [Object-Oriented Programming](https://dev.to/mzunairtariq/understanding-object-oriented-programming-concepts-a-comprehensive-guide-for-interviews-gjg) that involves the creation of a new [class](https://dev.to/mzunairtariq/understanding-object-oriented-programming-unveiling-the-power-of-classes-2kd0) (known as the derived class) by obtaining the attributes and functionalities of an existing class (referred to as the base class or superclass). The derived class has the ability to access and make use of the attributes and behaviors of the base class. This allows for the reuse of code and promotes the development of class hierarchies that can represent relationships seen in the real world.


Inheritance promotes the reuse of code, encourages the development of hierarchical links between classes, and supports the organization of code into logical structures, hence promoting modularity and extensibility in the design of software.

In Object-Oriented Programming (OOP), inheritance encompasses more than one type; various types of inheritance exist for establishing a relationship between classes. 

**The most common types of inheritance include:**

1. **Single Inheritance:** In single inheritance, a derived class inherits attributes and behaviours purely from a single base class. Every class can have a maximum of one parent class. This represents the most basic and simplest form of inheritance.

```lua
         +--------------+
         |    Vehicle   |
         +--------------+
               |
         +--------------+
         |    Car       |
         +--------------+

```
**Explanation:**

- **Vehicle** is the base class.
- **Car** is derived from **Vehicle**, inheriting characteristics from the Vehicle class.

2. **Multiple Inheritance:** Multiple inheritance allows a derived class to inherit properties and behaviors from multiple base classes. However, not all programming languages support multiple inheritance due to complications linked to unpredictability and method conflicts.

```lua
         +--------------+   +--------------+
         |    Class A   |   |    Class B   |
         +------+-------+   +------+-------+
                |                  |
         +------+-------+   +------+-------+
         |    Class C   |   |    Class D   |
         +------+-------+   +------+-------+
                      \     /
                       +---+
                       | E |
                       +---+

```
**Explanation:**

- Class **E** inherits from both Class **C** and Class **D**, representing multiple inheritance.
- Class **C** and Class **D** inherit from Class **A** and Class **B**, respectively.
- Class **E** indirectly inherits properties and behaviors from both Class **A** and Class **B** through Class **C** and Class **D**.

3. **Multilevel Inheritance:** Multilevel inheritance is a chain of inheritance where a derived class inherits from a base class, and another class inherits from this derived class, forming a hierarchical structure. This generates a parent-child relationship at multiple levels.

```lua
             +--------------+
             |   Vehicle    |
             +--------------+
                   /_\
                    |
             +--------------+
             |   Car        |
             +--------------+
                   /_\
                    |
            +--------------+
            |   SportsCar  |
            +--------------+

```
<u>Explanation:</u>

- **Vehicle** is the **base class** at the top level.
- **Car** is derived from **Vehicle**, inheriting characteristics from the **Vehicle class**.
- **SportsCar** further extends the hierarchy by inheriting from the **Car class**. It builds upon the properties and behaviors inherited from both **Vehicle and Car**.

4. **Hierarchical Inheritance:** In hierarchical inheritance, many different classes are derived from a single base or parent class. This results in a tree-like structure where one base class serves as the parent class for numerous derived classes.

```lua
             +--------------+
             |   Animal     |
             +------+-------+
                    |
         +----------+---------+
         |                    |
   +--------------+     +--------------+
   |   Mammal     |     |    Bird      |
   +------+-------+     +------+-------+
          |                      |
    +--------------+     +--------------+
    |    Dog       |     |    Eagle     |
    +--------------+     +--------------+

```
**Explanation:**

- **Animal** serves as the base class at the top level.
- **Mammal and Bird** classes inherit from **Animal**, representing two branches of the hierarchy, showcasing different types of animals.
- **Dog and Eagle** are further derived classes, each inheriting characteristics from their respective parent classes (Mammal and Bird).

5. **Hybrid (or Mixed) Inheritance:** Hybrid inheritance is a combination of different types of inheritance. It might be a mix of single, multiple, multilevel, or hierarchical inheritance depends on the programming language's support and implementation.

```lua
            +--------------+
            |    Vehicle   |
            +------+-------+
                   /_\
                    |
            +------+-------+
            |    Car       |
            +------+-------+
                  /     \
           +-----+       +-----+
           |  Sedan       |   Truck  |
           +------+       +-----+
                 |
           +------+-------+
           |    HybridCar  |
           +--------------+

```
**Explanation:**

- **Vehicle** serves as the base class.
- **Car, Sedan, and Truck** are derived classes inheriting from **Vehicle**, showcasing **single** inheritance.
- **Sedan and Truck** are **siblings**, both derived from **Car**, representing a hierarchical relationship.
- **HybridCar** is derived from **Sedan**, showcasing a multilevel inheritance where it extends the **Sedan** class.

**Please note** that this is a simplified picture, and in real terms, hybrid inheritance might include greater complexity with several inheritance types merged, potentially leading to issues such as the diamond problem or ambiguity in certain cases. Some programming languages might limit or avoid implementing certain types of hybrid inheritance due to these complications.