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

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 …
Read More

Java Program to Find pair of Integers in Array whose sum is given Number

In this tutorial, we will write a program to find a pair of elements from an array whose sum equals a given number in java …
Read More

Program to Print Diamond Alphabet Patterns in C

In this tutorial, we will learn to write a C program to print Diamond patterns using alphabets/characters. However, in this tutorial, we will create a …
Read More

Half Diamond Pattern in C using Alphabets

In this tutorial, we will learn and code the half diamond alphabet patterns in C programming language. However, in this tutorial, we will create a …
Read More

Half Pyramid of Alphabets in C

In this tutorial, we will learn and code alphabet patterns in C programming language specifically the Half pyramid of alphabets in C programming. However, in …
Read More

Inverted Half Pyramid Pattern of Alphabets in C

In this tutorial, we will write a C program to print half Pyramid using alphabets/characters. Before that, you may go through the following topic in …
Read More