Overview of Inheritance in C#

Introduction

 
In this blog, I will explain inheritance in C#. Inheritance is the most important pillar of OOPS (Object-Oriented Programming). The class whose members are inherited is called the 'base class', 'super class', or 'parent class'. The class that inherits those members is called the 'derived class', 'child class' or 'sub class'.
 
Inheritance is the base class for the derived class. It inherits all variables, methods, properties, and indexers defined by the base class and adds its own unique elements. Inheritance supports reusability by allowing us to extend an existing class. Inheritance also increases the functionality of a derived class and promotes the reusability of the code and base class. Inheritance is used if we want to create a class and reuse methods or properties from existing classes, then implement the inheritance.
 
The following types are used in inheritance: Single inheritance, Multi-level inheritance, multiple inheritances, Multipath inheritance, Hierarchical Inheritance, and Hybrid Inheritance.
 
Syntax
  1. class Car {  
  2. }  
  3. class Bike: Car {  
  4. }  
Example
  1. public class A {  
  2.  public void testA(int carSpeed, int Bikespeed) {  
  3.   
  4.   int carspeed = carSpeed;  
  5.   int bikespeed = Bikespeed;  
  6.   Console.WriteLine("Carspeed=" + carSpeed);  
  7.   Console.WriteLine("Bikespeed=" + bikespeed);  
  8.  }  
  9.   
  10. }  
  11. public class B: A {  
  12.  public void testB() {  
  13.   Console.WriteLine("Vechile Speed test");  
  14.  }  
  15. }  
  16. public class test {  
  17.  public void Main() {  
  18.   B speed = new B();  
  19.   speed.testA(80, 20);  
  20.  }