Types of Inheritance in C#

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.
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, which is possible by using inheritance.

The following are the types of inheritance in C#.

Types of Inheritance

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.

Single inheritance
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 for one class feature that is needed in multiple classes.

Hierarchical inheritance

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 into many classes, hence it is called a Hierarchical Inheritance.

Multilevel inheritance


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

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..

Multiple inheritance using Interfaces

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.
Summary

I hope you now have an overview of inheritance and its types. In the next article, we will learn about each inheritance type in detail. If you have any suggestions regarding this article, then please contact me.

Post a Comment

www.CodeNirvana.in

Protected by Copyscape
Copyright © Compilemode