Inheritance implementation in C#

 In object-oriented programming, Inheritance is one of the most important concepts because it allows the creation of hierarchical classifications. It is the mechanism by which one class is allowed to inherit the features(fields, properties, indexers, and methods) of another class. It provides the ability to reuse the same code, instead of writing the same code again and again.

In C#, a class that is inherited is called a base class. The class that does the inheriting is named a derived class. Therefore, a derived class is a specialized version of a base class. It inherits all of the fields, methods, properties, and indexers defined by the base class  (except private members )and adds its own unique elements.

 Inheritance supports the concept of reusability. When we want to create a new class and there is already a class that includes some common code that we want, we can derive our new class from the existing class. By doing this, we are reusing the fields, methods, properties, and indexers of the existing class.

In C#, we can inherit a class with the help of the colon operator (:).

C# supports the following four types of inheritance:

  1.  Single Inheritance. 
  2. Multilevel Inheritance.
  3.  Hierarchical Inheritance. 
  4. Multiple Inheritance (only with interfaces).


Types of inheritance in c#










Single Inheritance

In a single-level inheritance, there is one base class and one derived class, as given below:

Single-level inheritance


Here, class B is derived from class A.


Let's take an example to implement this in C#:



Multi-level Inheritance


In a multi-level inheritance, A class is derived from another class in a chain way, as given below:

Multi-level Inheritance


Here, class B is derived from class A, and further class C is derived from class B.

Let's take an example to implement this in C#:



Hierarchical Inheritance

In a hierarchical inheritance, multiple classes are derived from the same base class, as given below:

Hierarchical Inheritance




Here, both class B and C are derived from class A.


Let's take an example to implement this in C#:


Multiple Inheritances


In multiple inheritances, a class has multiple base classes. C# doesn't allow to inherit multiple classes, but allows to inherit multiple interfaces with or without a single base class, as given below:

Multiple Inheritances




Here, class C is derived from class A and interface IB.

C# compiler is designed not to support multiple inheritance because it causes ambiguity of methods from different base class.


Let's take an example to implement this in C#:

Vikash Chauhan

C# & .NET experienced Software Engineer with a demonstrated history of working in the computer software industry.

Post a Comment

Previous Post Next Post

Contact Form