C# Inheritance

Inheritance in C# is known as the concept which can be used to inherit properties like fields & methods, from one class to another class. Due to this inheritance classes are divided into 2 classes.

Real World Example

A scientific calculator is a type of calculator that has been extended. The parent object is the calculator, while the child object is the scientific calculator.

Example in Programming

The process of a child object inheriting the parent’s existing capabilities as well as new features and functionality.

Inheritance is a notion in Object Oriented Programming in which one class can inherit the properties and methods of another.

  • Base Class (Also known as Parent class)
  • Derived Class (Also known as Child class)

The inheriting class is usually referred to as a derived class. And the inherited class is known as the base class.

The attributes and methods of the base class can be used, extended, or overridden by the derived class.

When your use cases involve categorical and sub-categorical objects, inheritance can be used.

C# Inheritance Syntax:

Instead of developing entirely new data members and member functions when creating a class, the programmer can specify that the new class should inherit the members of an existing class. The old class is known as the base class, while the new class is known as the derived class.

The IS-A relationship is implemented using the concept of inheritance. For example, a mammal is an animal, and a dog is a mammal, ergo a dog is an animal as well.

To better understand the Derived and Base Class observe the example given below,

using System;

namespace CsharpInheritance {
   class Area {
      public void setSide(int s) {
         Side = s;
      }
     
      protected int Side;
      }

   // Derived class
   class Square: Area {
      public int getArea() { 
         return (Side * Side); 
      }
   }
  public class SquareTester {
   public   static void Main(string[] args) {
         Square Squ = new Square();

         Squ.setSide(18);
       
         // Print the area of the object.
         Console.WriteLine("Area of Square: {0}",  Squ.getArea());
         
      }
   }
}

Inheritance Types:

Inheritance has the following types in C#.

1- Single Level Inheritance:

Inheritance in which we have only one base class and one derived class is known as Single level Inheritance. The derived class can inherit the properties of the base class.

Shown in the image:

C# Inheritance single level
using System;  
   public class Student 
    {  
       public int id = 7;  
   }  
   public class Class: Student  
   {  
       public int Marks = 89;  
   }  
 public  class CsharpTestInheritance{  
       public static void Main(string[] args)  
        {  
            Class c1 = new Class();  
  
            Console.WriteLine("ID = " + c1.id);  
            Console.WriteLine("Marks = " + c1. Marks);  
  
        }  
    }  

2- Multilevel Inheritance:

Multilevel inheritance has only one base class but has multiple derived classes.  The derived class also act as the base class to other class.

Shown in the image:

C# Inheritance multilevel
using System;  
   public class Lisa  
    {  
       public void marks() { Console.WriteLine("Lisa's Marks=83"); }  
   }  
   public class John:Lisa 
   {  
       public void marks1() { Console.WriteLine("John's Marks=76"); }  
   }  
   public class Alice:John   
   {  
       public void marks2() { Console.WriteLine("Alice's Marks=59"); }  
   }  
  public class CsharpTestInheritance2{  
       public static void Main(string[] args)  
        {  
            Alice s1 = new Alice();  
            s1.marks();  
            s1.marks1();  
            s1.marks2();  
        }  
    }  

3- Multiple Inheritance:

Multiple inheritances have one class that can have more than one superclass and inherit features from all base classes. In C#, we can achieve multiple inheritances only through Interfaces.

Shown in the image:

multiple

4- Hirerachical Inheritance:

In Hierarchical Inheritance, one class serves as a base class for more than one subclass.

Shown in the image:

hirerachy