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

C Program to search an element in an array using Pointers

A separate function( search_function()) will be created where the array pointer will be declared and the searched element along with the size of an array …

C Program to find the sum of the digits of a number using recursion function

This C program calculates the sum of digits of a given number using recursion. Here’s a concise explanation: Function Definition: sumDigits(int n) This function calculates …

C program to find factorial of a numberĀ using Ternary operator with Recursion

Recursion refers to the function calling itself directly or in a cycle. Before we begin, you should have the knowledge of following in C Programming: …

C Program to Add Two Numbers Using Call by Reference

The program takes the two numbers from the user and passes the reference to the function where the sum is calculated. You may go through …

Find the output ab, cd, ef, g for the input a,b,c,d,e,f,g in Javascript and Python

In this tutorial, we will write a program to find a pairs of elements from an array such that for the input [a,b,c,d,e,f,g] we will …

String Pattern Programs in C

In this tutorial, we will write various C pattern programs for String. Before that, you may go through the following topics in C. for loop …