Types of Inheritance in C#

Background

In this article we will learn about one of the most reusable object oriented features of C#, inheritance. We will learn about inheritance from the basics because I have written this article focusing on students and beginners. Before proceeding further please refer to my previous articles for a better understanding.
  • Programming Methodologies
  • Types of Classes in C#
  • Polymorphism in C#
What Inheritance is
Acquiring (taking) the properties of one class into another class is called inheritance. Inheritance provides reusability by allowing us to extend an existing class.
The reason behind OOP programming is to promote the reusability of code and to reduce complexity in code and it is possible by using inheritance.



The following are the types of inheritance in C#.



The inheritance concept is based on a base class and derived class. Let us see the definition of a base and derived class.
Base class: is the class from which features are to be inherited into another class.
Derived class: it is the class in which the base class features are inherited.

Single inheritance

It is the type of inheritance in which there is one base class and one derived class.


For example:
public class Accountcreditinfo //base class  
{
    public string Credit()
    {
        return "balance is credited";
    }
}
public class debitinfo : Accountcreditinfo //derived class  
{
    public string debit()
    {
        Credit();                       ////derived class method  
        return "balance is debited";
    }
}
In the preceding sample program Accountcreditinfo is the base class and debitinfo is the derived class.

Hierarchical inheritance

This is the type of inheritance in which there are multiple classes derived from one base class. This type of inheritance is used when there is a requirement of one class feature that is needed in multiple classes.
 



For example:
class A  //base class  
{
    public string msg()
    {
        return "this is A class Method";
    }
}
class B : A
{
    public string info()
    {
        msg();
        return "this is B class Method";
    }
    class C : A
    {
        public string getinfo()
        {
            msg();
            return "this is B class Method";
        }
    }
 
In the preceding program one base class is derived in many classes hence it is a called a Hierarchical Inheritance.

Multilevel inheritance

When one class is derived from another derived class then this type of inheritance is called multilevel inheritance.



For example:
public class Person
{
    public string persondet()
    {
        return "this is the person class";
    }
}
public class Bird : Person
{
    public string birddet()
    {
        persondet();
        return "this is the birddet Class";
    }
}
public class Animal : Bird
{
    public string animaldet()
    {
        persondet();
        birddet();
        return "this is the Animal Class";
    }
} 
In the preceding program, each class is derived from one class that is derived from another class hence this type of inheritance is called Multilevel Inheritance.

Multiple inheritance using Interfaces

C# does not support multiple inheritances of classes. To overcome this problem we can use interfaces, we will see more about interfaces in my next article in detail.



For example:
public interface IA //ineterface  1  
{
    string setImgs(string a);
}
public interface IB  //Interface 2  
{
    int getAmount(int Amt);
}
public class ICar : IA, IB //implementatin  
{
    public int getAmount(int Amt)
    {
        return 100;
    }
    public string setImgs(string a)
    {
        return "this is the car";
    }
 
In the preceding program the ICar class inherits the features of the two interfaces hence this type of inheritance is called Multiple Inheritance.

The following are some key points about inheritance:

  1. C# does not support multiple inheritances of classes, the same thing can be done using interfaces.
  2. Private members are not accessed in a derived class when one class is derived from another.