> For the complete documentation index, see [llms.txt](https://raviram.gitbook.io/c-programing/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://raviram.gitbook.io/c-programing/oop-part-2/c-inheritance-and-overloading.md).

# C# Inheritance & Overloading

## C# Inheritance

In C#, inheritance allows us to create a new class from an existing class. It is a key feature of Object-Oriented Programming (OOP).

The class from which a new class is created is known as the base class (parent or superclass). And the new class is called derived class (child or subclass)

{% hint style="info" %}
The derived class inherits the **fields and methods** of the base class. This helps with the code reusability in C#.
{% endhint %}

### How to perform inheritance in C#?

In C#, we use the `:` symbol to perform inheritance. For example,

```csharp
class Animal {  
  // fields and methods
} 

// Dog inherits from Animal
class Dog : Animal {
  // fields and methods of Animal 
  // fields and methods of Dog 
}
```

Here, we are inheriting the derived class `Dog` from the base class `Animal`. The `Dog` class can now access the fields and methods of `Animal` class.

**Example: C# Inheritance**

```csharp
using System;

class Program
{
    static void Main(string[] args)
    {
        // object of derived class
        Dog labrador = new Dog();

        // access field and method of base class
        labrador.name = "Rohu";
        labrador.DisplayInfo();

        //child's own method
        labrador.ShowName();
        Console.ReadLine();
    }
}

// base class
class Animal
{
    public string name;

    public void DisplayInfo()
    {
        Console.WriteLine("I am an animal");
    }
}

//derived class
class Dog: Animal
{
    public void ShowName()
    {
        Console.WriteLine("My name is " + name);
    }
}
```

Here, we are using `labrador` (object of Dog) to access the `name` and `display()` of the `Animal` class. This is possible because the derived class inherits all fields and methods of the base class.

Also, we have accessed the `name` field inside the method of the `Dog` class.

### is-a relationship

In C#, inheritance is an `is-a relationship`. We use inheritance only if there is an `is-a` relationship between two classes. For example,

* **Dog** is an **Animal**
* **Apple** is a **Fruit**
* **Car** is a **Vehicle**

We can derive **Dog** from **Animal** class. Similarly, **Apple** from **Fruit** class and **Car** from **Vehicle** class.

### Protected Members in C# Inheritance

When we declare a field or method as `protected`, it can only be accessed from the class and its derived classes.

**Example: protected Members in Inheritance**

```csharp
using System;
class Program
{
    static void Main(string[] args)
    {
        Dog labrador = new Dog();

        // wrong - cannot access protected method from outside child class
        labrador.eat();

        Console.ReadLine();
    }
}
// base class
class Animal
{
    protected void eat()
    {
        Console.WriteLine("I can eat...");
    }
}

class Dog : Animal
{

}
```

In the above example, we have created a class named `Animal`. The class includes a protected method `eat()`.

We have derived the `Dog` class from the `Animal` class.&#x20;

Notice the statement `labrador.eat();`&#x20;

### Types of inheritance

There are the following types of inheritance:

### 1.Single Inheritance

In single inheritance, a single derived class inherits from a single base class.

<figure><img src="/files/3OvqOqeWBeRbydUBoao4" alt=""><figcaption></figcaption></figure>

### 2.Multilevel Inheritance

In multilevel inheritance, a derived class inherits from a base and then the same derived class acts as a base class for another class.

<figure><img src="/files/hw0ZrR51eUJ1jWQrjzGC" alt=""><figcaption></figcaption></figure>

### 3.Hierarchical Inheritance

In hierarchical inheritance, multiple derived classes inherit from a single base class.

<figure><img src="/files/VtoCOkVGWk2A38YiaW3l" alt=""><figcaption></figcaption></figure>

### 4.Multiple Inheritance (not supported)

In multiple inheritance, a single derived class inherits from multiple base classes. **C# doesn't support multiple inheritance.** However, we can achieve multiple inheritance through interfaces.

<figure><img src="/files/lFoxBGxlzx7sCHBrqBpp" alt=""><figcaption></figcaption></figure>

### 5.Hybrid Inheritance

Hybrid inheritance is a combination of two or more types of inheritance. The combination of multilevel and hierarchical inheritance is an example of Hybrid inheritance.

<figure><img src="/files/z4QBnLcOdyvqVyiU95R6" alt=""><figcaption></figcaption></figure>

## Method Overriding in C# Inheritance

If the same **method is present in both the base class and the derived class**, the method in the derived class overrides the method in the base class. This is called method overriding in C#. For example,

```csharp
using System;

// base class
class Animal
{
    public virtual void eat()
    {
        Console.WriteLine("I eat food");
    }
}
// derived class of Animal 
class Dog : Animal
{
    // overriding method from Animal
    public override void eat()
    {
        Console.WriteLine("I eat Dog food");
    }
}
class Program
{
    static void Main(string[] args)
    {
        // object of derived class
        Dog labrador = new Dog();

        // accesses overridden method
        labrador.eat();
    }
}
```

In the above example, the `eat()` method is present in both the base class and derived class.

When we call `eat()` using the `Dog` object `labrador`,

`labrador.eat();` the method inside `Dog` is called. This is because the method inside `Dog` overrides the same method inside `Animal`.

Notice, we have used `virtual` and `override` with methods of the base class and derived class respectively. Here,

* `virtual` - allows the method to be overridden by the derived class
* `override` - indicates the method is overriding the method from the base class

### `base` Keyword in C# Inheritance

In the previous example, we saw that the method in the derived class overrides the method in the base class.

**However, what if we want to call the method of the base class as well?**

In that case, we use the `base` keyword to call the method of the base class from the derived class.

```csharp
using System;

// base class
class Animal
{
    public virtual void eat()
    {
        Console.WriteLine("I eat food");
    }
}
// derived class of Animal 
class Dog : Animal
{
    // overriding method from Animal
    public override void eat()
    {
        // call method from Animal class
        base.eat();
        Console.WriteLine("I eat Dog food");
    }
}
class Program
{
    static void Main(string[] args)
    {
        // object of derived class
        Dog labrador = new Dog();

        // accesses overridden method
        labrador.eat();
    }
}
```

In the above example, the `eat()` method is present in both the base class `Animal` and the derived class `Dog`. Notice the statement,

`base.eat();` Here, we have used the `base` keyword to access the method of `Animal` class from the `Dog` class.
