C# Inheritance and its Types4 min read

Inheritance is an important pillar for the foundation of Object-Oriented Programming (OOP). It is a process in C# through which one class can acquire the fields and methods of another class.

Inheritance allows us to inherit or acquire the features of the parent class to the child class so that we can be reuse, extend or modify the attributes and behaviour of another class.

Importance of Inheritance:

  • For Code Reusability.
  • For Method Overriding (to achieve runtime polymorphism).

Important terms related to inheritance.

  • Super Class: The class whose properties and functionalities are inherited by the Subclass is known as superclass(a parent class or a base class).
  • Sub Class: The class that inherits the property and behavior of the other class(Superclass) is known as subclass( a derived class, extended class, or child class). The subclass can have its own fields and methods along with the fields and methods of the superclass.

Syntax to use inheritance in C#


Types of Inheritance in C#

Below are the various types of inheritance supported in C#.

1. Single Inheritance

It refers to an inheritance where a child inherits a single-parent class. In the diagram below, class B inherits class A.

single Inheritance

Syntax:

Example: C# example for single inheritance.

//Output:
This is Parent Class
This is Child Class


2. Hierarchical Inheritance

The process of deriving more than one class from a base class is called hierarchical inheritance. In other words, we create more than one derived class from a single base class as shown in the diagram below.

hierarchical Inheritance

Syntax:

Example: C# example for hierarchical Inheritance.

//Output
This is Son's Class
This is Parent Class
This is Daughter's Class
This is Parent Class


3. Multilevel Inheritance

In this type inheritance derived class is created from another derived class. In multilevel inheritance, one class inherits from another class which is further inherited by another class. The last derived class possesses all the members of the above base classes.

Multilevel Inheritance

Example: C# example for multilevel inheritance.

//Output
Animal sleeps
Dog barks
Puppy weeps


4. Multiple Inheritance (through Interface)

Multiple inheritance is the process where the single class inherits the properties from two or more classes.

In the diagram below, class C inherits from both Class A and Class B.

multiple Inheritance

Although, C# does not support multiple inheritance. So we need to use interfaces in C# to use the multiple inheritance. We need to declare the parent class with the interface keyword and the child class with the class keyword.

Example: C# example for multiple inheritance.

All are Animals
I am a Dog
I am A Cat


MORE

Keith Number Program in C

In this tutorial, we will write a Keith number in C. It is one of the most asked questions in an interview. Before that, you …
Read More

Keith Number Program in C++

In this tutorial, we will write a Keith number in C++. It is one of the most asked questions in an interview. Before that, you …
Read More

Singly linked list Program C++

In this tutorial, we will write a C++ program to implement singly linked list. In this program, we will create a separate function for each …
Read More

Fascinating Number Program in C

In this tutorial, we will write a C program to check whether the given number is a Fascinating number or not. You may go through …
Read More

Peterson Number in C

In this tutorial, we will learn about the Peterson number and check if the number is Peterson or not in C. What is Peterson Number? …
Read More

Peterson Number in C++

In this tutorial, we will learn about the Peterson number and write a C++ program to check if the given number is a Peterson number …
Read More