C# Inheritance

Inheritance is one of the four pillars of object-oriented programming (OOP), alongside encapsulation, abstraction, and polymorphism. It allows a class (called a subclass or derived class) to inherit properties, methods, and other members from parent class.

Types of Inheritance in C#

1. Single Inheritance

2. Multilevel Inheritance

3. Hierarchical Inheritance

4. Multiple Inheritance (via Interfaces)

5. Interface Inheritance

1. Single Inheritance

In single inheritance, a class inherits from one and only one base class. This is the most common form of inheritance.

Example:


using System;

class Animal {
    public void eat() {
         Console.WriteLine("Animal is eating");
    }
}

class Dog : Animal {
    public void bark() {
         Console.WriteLine("Dog is barking");
    }
}

public class ProgramExample {
    public static void Main() {
        Dog dog = new Dog();
        dog.eat();   // Inherited from Animal
        dog.bark();  // Inherited from Dog
    }
}

Output:

Animal is eating
Dog is barking

Explanation:

In this example, Dog class inherits from Animal class , meaning it can access the eat method defined in Animal. Dog also has its own method, bark.

2. Multilevel Inheritance

In multilevel inheritance, a class derives from another derived class. This creates a chain of inheritance.

Example:


using System;

class Animal {
    public void eat() {
         Console.WriteLine("Animal is eating");
    }
}

class Dog : Animal {
    public void bark() {
         Console.WriteLine("Dog is barking");
    }
}

class PitBull : Dog {
    public void quality() {
         Console.WriteLine("Pit Bull is heighly intelligent");
    }
}

public class ProgramExample {
    static void Main() {
        PitBull pibull = new PitBull();
        pibull.eat();   // Inherited from Animal
        pibull.bark();  // Inherited from Dog
        pibull.quality(); // Inherited from PitBull
    }
}

Output:

Animal is eating
Dog is barking
Pit Bull is heighly intelligent

Explanation:

  1. PitBull inherits from Dog.
  2. Dog inherits from Animal. So, PitBull indirectly inherits from Animal through Dog.

3. Hierarchical Inheritance

In hierarchical inheritance, multiple derived classes inherit from a single base class. The base class is shared among several derived classes.


using System;
// Parent class
class Animal {
    public void eat() {
        Console.WriteLine("This animal eats food.");
    }
}

// Child class 1
class Dog : Animal {
    public void bark() {
        Console.WriteLine("The dog barks.");
    }
}

// Child class 2
class Cat : Animal {
    public void meow() {
        Console.WriteLine("The cat meows.");
    }
}

 class ProgramExample {
     static void Main() {
        Dog dog = new Dog();
        dog.eat(); // Inherited from Animal
        dog.bark(); // Defined in Dog

        Cat cat = new Cat();
        cat.eat(); // Inherited from Animal
        cat.meow(); // Defined in Cat
    }
}

Output:

This animal eats food.
The dog barks.
This animal eats food.
The cat meows.

Explanation: In this example, both Dog and Cat inherit the eat() method from the Animal class.

4. Multiple Inheritance (via Interfaces)

In C#, a class can implement multiple interfaces. C# does not support multiple class inheritance directly, but it allows a class to inherit from multiple interfaces. This is known as multiple inheritance via interfaces.

Example:


using System;
// First interface
interface Eatable {
    void eat();
}

// Second interface
interface Runnable {
    void run();
}

// Child class implementing both interfaces
class Dog : Eatable, Runnable {
    public void eat() {
        Console.WriteLine("The dog eats food.");
    }

    public void run() {
       Console.WriteLine("The dog runs fast.");
    }
}

class MyProgram {
     static void Main() {
        Dog dog = new Dog();
        dog.eat(); // Implemented from Eatable interface
        dog.run(); // Implemented from Runnable interface
    }
}

Output:

The dog eats food.
The dog runs fast.

Explanation:

  1. Dog implements both Eatable and Runnable interfaces.
  2. This allows the Dog class to inherit behavior from multiple sources, providing a form of multiple inheritance.

5. Interface Inheritance

Interface inheritance allows one interface to inherit from another. This enables the creation of more specific interfaces that extend the behavior of a general interface.

Example:


using System;
// First interface
interface Animal {
    void eat();
}
// Second interface
interface Runnable : Animal {
    void run();
}


// Dog implement Runnable interfaces
class Dog : Runnable {
    public void eat() {
        Console.WriteLine("The dog eats food.");
    }

    public void run() {
       Console.WriteLine("The dog runs fast.");
    }
}

class MyProgram {
     static void Main() {
        Dog dog = new Dog();
        dog.eat(); // Implemented from Eatable interface
        dog.run(); // Implemented from Runnable interface
    }
}

Output:

The dog eats food.
The dog runs fast.

Explanation:

Here, Runnable extends Animal, so any class implementing Runnable must implement all methods from both Animal and Runnable. In this case, Dog implements both eat() and run() method.