Types of Inheritance


In this article we are going to discuss the Types of Inheritance in C#. There are many blogs, articles are available on the internet regarding Types of Inheritance but in this particular article I will try to explain to you with as much as simplest and realistic examples so you can get a clear idea of the Types of Inheritance in C#.

Types of Inheritance in C#

Below are the different types of inheritance which is supported by C# in different combinations.

Types of Inheritance

Single Inheritance

In single inheritance, subclasses inherit the features of one superclass. In image below, the class A serves as a base class for the derived class B.

Types of Inheritance
public class Animal
{
    public void Eat()
    {
        Console.WriteLine("Eating...");
    }
}

public class Dog : Animal
{
    public void Run()
    {
        Console.WriteLine("Runing...");
    }
}

// Example usage
class Program
{
    static void Main(string[] args)
    {
        Dog dog = new Dog();
        dog.Eat();  // Inherited from Animal
        dog.Run(); // Defined in Dog
    }
}

Output

Types of Inheritance

Multilevel Inheritance

In Multilevel Inheritance, a derived class will be inheriting a base class and as well as the derived class also act as the base class to other class. In below image, class A serves as a base class for the derived class B, which in turn serves as a base class for the derived class C.

Types of Inheritance

Example

public class Animal
{
    public void Eat()
    {
        Console.WriteLine("Eating...");
    }
}

public class Dog : Animal
{
    public void Run()
    {
        Console.WriteLine("Runing...");
    }
}
public class Cat : Dog
{
    public void Speak()
    {
        Console.WriteLine("Speaking...");
    }
}

// Example usage
class Program
{
    static void Main(string[] args)
    {
        Cat cat = new Cat();
        cat.Eat();  // Inherited from Animal
        cat.Run(); // Defined in Dog
        cat.Speak(); // Defined in Cat
    }
}

Output

Types of Inheritance

Hierarchical Inheritance

In Hierarchical Inheritance, one class serves as a superclass (base class) for more than one subclass. In below image, class A serves as a base class for the derived class B, C, and D.

Types of Inheritance

Example

public class Animal
{
    public void Eat()
    {
        Console.WriteLine("Eating...");
    }
}

public class Dog : Animal
{
    public void Run()
    {
        Console.WriteLine("Runing...");
    }
}
public class Cat : Animal
{
    public void Speak()
    {
        Console.WriteLine("Speaking...");
    }
}

// Example usage
class Program
{
    static void Main(string[] args)
    {
        var dog = new Dog();
        var cat = new Cat();
        dog.Eat();  // Inherited from Animal
        cat.Eat();  // Inherited from Animal
        dog.Run(); // Defined in Dog
        cat.Speak(); // Defined in Cat
    }
}

Output

Types of Inheritance

Does C# support Multiple Inheritance?

NO. C# support inheritance by using multiple Interfaces. This is an alternative way of multiple inheritance.

Multiple Inheritance(Through Interfaces)

In Multiple inheritance, one class can have more than one superclass and inherit features from all parent classes. Please note that C# does not support multiple inheritance with classes. In C#, we can achieve multiple inheritance only through Interfaces. In the image below, Class C is derived from interface A and B.

Types of Inheritance

Example

public interface IAnimal
{
    public void Eat();
    
}

public interface IBird
{
    public void Fly();

}
public class Parrot : IAnimal, IBird
{
    public void Eat()
    {
        Console.WriteLine("Eating...");
    }
    public void Fly()
    {
        Console.WriteLine("Fly...");
    }
}


// Example usage
class Program
{
    static void Main(string[] args)
    {
        Parrot parrotobj = new Parrot();
        parrotobj.Eat();
        parrotobj.Fly();
    }   
}

Output

ImageRun4

Are Private Class Members Inherited to the Derived Class?

No, the private members cannot be inherited to derived class. Only public and protected class members can be inherited.


Next