C Sharp Inheritance

Education is not limited to just classrooms. It can be gained anytime, anywhere... - Ravi Ranjan (M.Tech-NIT)

C# Inheritance

In C#, inheritance is a process in which one object acquires all the properties and behaviors of its parent object automatically. In such way, you can reuse, extend or modify the attributes and behaviors which is defined in other class.

In C#, the class which inherits the members of another class is called derived class and the class whose members are inherited is called base class. The derived class is the specialized class for the base class.

Advantage of C# Inheritance

Code reusability: Now you can reuse the members of your parent class. So, there is no need to define the member again. So less code is required in the class.

C# Single Level Inheritance Example: Inheriting Fields

When one class inherits another class, it is known as single level inheritance. Let's see the example of single level inheritance which inherits the fields only.

  1. using System;  
  2.    public class Employee  
  3.     {  
  4.        public float salary = 40000;  
  5.    }  
  6.    public class Programmer: Employee  
  7.    {  
  8.        public float bonus = 10000;  
  9.    }  
  10.    class TestInheritance{  
  11.        public static void Main(string[] args)  
  12.         {  
  13.             Programmer p1 = new Programmer();  
  14.   
  15.             Console.WriteLine("Salary: " + p1.salary);  
  16.             Console.WriteLine("Bonus: " + p1.bonus);  
  17.   
  18.         }  
  19.     }  

Output:

Salary: 40000
Bonus: 10000

In the above example, Employee is the base class and Programmer is the derived class.

C# Single Level Inheritance Example: Inheriting Methods

Let's see another example of inheritance in C# which inherits methods only.

  1. using System;  
  2.    public class Animal  
  3.     {  
  4.        public void eat() { Console.WriteLine("Eating..."); }  
  5.    }  
  6.    public class Dog: Animal  
  7.    {  
  8.        public void bark() { Console.WriteLine("Barking..."); }  
  9.    }  
  10.    class TestInheritance2{  
  11.        public static void Main(string[] args)  
  12.         {  
  13.             Dog d1 = new Dog();  
  14.             d1.eat();  
  15.             d1.bark();  
  16.         }  
  17.     }  

Output:

Eating...
Barking...

C# Multi Level Inheritance Example

When one class inherits another class which is further inherited by another class, it is known as multi level inheritance in C#. Inheritance is transitive so the last derived class acquires all the members of all its base classes.

Let's see the example of multi level inheritance in C#.

  1. using System;  
  2.    public class Animal  
  3.     {  
  4.        public void eat() { Console.WriteLine("Eating..."); }  
  5.    }  
  6.    public class Dog: Animal  
  7.    {  
  8.        public void bark() { Console.WriteLine("Barking..."); }  
  9.    }  
  10.    public class BabyDog : Dog  
  11.    {  
  12.        public void weep() { Console.WriteLine("Weeping..."); }  
  13.    }  
  14.    class TestInheritance2{  
  15.        public static void Main(string[] args)  
  16.         {  
  17.             BabyDog d1 = new BabyDog();  
  18.             d1.eat();  
  19.             d1.bark();  
  20.             d1.weep();  
  21.         }  
  22.     }  

Output:

Eating...
Barking...
Weeping...

 

Note: Multiple inheritance is not supported in C# through class.