C#

Inheritance in C Sharp



Inheritance is an important pillar of OOP(Object Oriented Programming). It is the mechanism in C# by which one class is allowed to inherit the features(fields and methods) of another class.
It allows you to define a child class that reuses (inherits), extends, or modifies the behaviour of a parent class. The class whose members are inherited is called the base class. The class that inherits the members of the base class is called the derived class.

Please see following link for other OOPs feature.
What is Encapsulation in C#
What is Polymorphism in C#
What is Abstraction in C#

Why we use inheritance in C #

The  re-usability of code & reduce complexity in code and it is possible by using inheritance.
Important terminology:
Base Class: The class whose features are inherited is known as super class(or a base class or a parent class).
Child Class: The class that inherits the other class is known as subclass(or a derived class, extended class, or child class). The subclass can add its own fields and methods in addition to the super class fields and methods.
Reusability & Reduce Time Complexity: Inheritance supports the concept of “reusability”, i.e. when we want to create a new class and there is already a class that includes some of the code that we want, we can derive our new class from the existing class(i.e reduce time complexity). By doing this, we are reusing the fields and methods of the existing class.

Types of Inheritance in C#
Below are the different types of inheritance which is supported by C# in different combinations.
      1. Single Inheritance: In single inheritance, subclasses inherit the features of one super class. In image below, the class A serves as a base class for the derived class B.

single Inheritance in c sharp

 2. Multilevel Inheritance: In Multilevel Inheritance, a derived class will be inheriting a base class and as well as the derived class also act as the base class to other class. In below image, class A serves as a base class for the derived class B, which in turn serves as a base class for the derived class C.

Multilevel Inheritance in c sharp

      3. Hierarchical Inheritance: In Hierarchical Inheritance, one class serves as a super class (base class) for more than one subclass. In below image, class A serves as a base class for the derived class B, C, and D.

Hierarchical Inheritance in c sharp

      4. Multiple Inheritance(Through Interfaces):In Multiple inheritance, one class can have more than one super class and inherit features from all parent classes. Please note that C# does not support multiple inheritance with classes. In C#, we can achieve multiple inheritance only through Interfaces. In the image below, Class C is derived from interface IA and IB.

Multiple Inheritance in c sharp

Note :-  A subclass does not inherit the private members of its parent class. However, if the super class has properties(get and set methods) for accessing its private fields, then a subclass can inherit.

1 comment: