C# Inheritance - Single, Hierarchical, Multilevel & Multiple Inheritance

    1 Votes


Inheritance is one of the important characteristic of the object oriented programming. It enables us to create new classes that can be reused, extended and modify the behaviour which is defined in the other classes. The class members which are inherited are known as base class and the class which inherits those members are known as derived class.

The derived class can have only one direct base class. Inheritance is transitive also. Like, if class Z is derived from class Y and class Y is derived from class X, so class Z can inherit the members of class Y and X.

Advantages:
  • It reduces the redundancy of code.
  • It provides the reusability of code.
  • It reduces the code size and improves the code readability.
  • It manages the code easily and divides the class into base class and derived class.
Disadvantages:
  • In inheritance the base class and child class are tightly coupled so if the base class is modified then it will affect all the child classes.
  • In the class hierarchy some of the data members are unused so the memory allocated to them remain unutilized which affects the performance of our program.

 

Base Class

In C# a class which is inherited is the base class and the class which inherits the members of the base class is known as derived class, it inherits all members except the constructor. Let us understand through an example:

using System;
namespace Application
{
   class Square
   {
      //member variables
      protected int side;
      public Square(int s)
      {
         side = s;
      }
      public int GetArea()
      {
         return side * side;
      }
      public void Display()
      {
         Console.WriteLine("Side is: {0}", side);
         Console.WriteLine("The area is: {0}", GetArea());
      }
   }//end of class Square
   class Screen : Square
   {
      private int price;
      public Screen(int s) : base(s)
      { }
      public int GetPrice ()
      {
         int price;
         price = GetArea () * 10;
         return price;
      }
      public void Display()
      {
         base.Display ();
         Console.WriteLine ("The price is: {0}", GetPrice ());
      }
   }
   class square
   {
      static void Main(string[] args)
      {
         Screen s = new Screen (5);
         s.Display();
         Console.ReadLine ();
      }
   }
}

Output:

Side is: 5
The area is: 25
The price is: 250

Types of Inheritance

There are some of the following inheritances:

  • Single Inheritance
  • Hierarchical Inheritance
  • Multilevel Inheritance
  • Multiple Inheritance

Single Inheritance

In this type of inheritance there is only one base class and one derived class. Let us have a look on the example:

using System;
namespace Application
{
	class A
	{
	public void show() 
	{
	 Console.WriteLine("Welcome to the world of C#");
	}
}
class B : A //class B is derived by class A
{
 public void display() 
 {
  Console.WriteLine("Hello");  
 }
}
class single
{
 public static void Main()
 {
  B obj = new B();
  obj.show();
  obj.display();
 }
}
}

Output:

Welcome to the world of C#
Hello

Hierarchical Inheritance

In this type of inheritance there are multiple classes which are derived from one base class. It is used when one class feature is required in multiple classes. Let us have a look on the example:

using System;
namespace Application
{
 class A
 {
  public void show()
  {
   Console.WriteLine("Welcome to the world of c#");
 }
}
 class B : A //class B is derived by class A
 {
  public void display()
  {
   Console.WriteLine("Hello");
  }
 }
 class C : A //class C is derived by class A
 {
  public void show1()
  {
   Console.WriteLine("How are You ?");
  }
 }
 class hierarchical
 {
  public static void Main()
  {
   B objl = new B();
   C obj2 = new C();
   objl.show();
   objl.display();
   obj2.show1();
  }
 }
}

Output:

Welcome to the world of c#
Hello
How are You ?

Multilevel Inheritance

In this type of inheritance one class is derived from another derived class. Let us have an example:

using System;
namespace Application 
{
class A
{
 public void show()
 {
  Console.WriteLine("Welcome to the world of c#");
 }
}
class B : A //class B is derived by class A
{
 public void display()
 {
  Console.WriteLine("Hello");
 }
}
class C : B //class C is derived by class B
{
 public void show1()
 { 
    Console.WriteLine("How are You ?");
 }
class multilevel
{
 public static void Main()
 {
  C obj = new C();
  obj.show();               // super class member function
  obj.display();                 // base class member function
  obj.show1();                 // own member function
 }
}
}
}

Output:

Welcome to the world of c#
Hello
How are You ?

Multiple Inheritance

As this type of inheritance is not supported by C# so to solve this problem we use interfaces. Let us have an example:

using System;
namespace Application
{
   class Shape 
   {
      public void setSide(int s)
      {
         side = s;
      }
     protected int side;
    }
   // Base class Cost
   public interface Cost 
   {
      int getCost(int area);
   }
   // Derived class
   class square : Shape, Cost
   {
      public int getArea()
      {
         return (side * side);
      }
      public int getCost(int area)
      {
         return area * 10;
      }
   }
   class Square
   {
      static void Main(string[] args)
      {
         square sq = new square();
         int area;
         sq.setSide(5);
         area = sq.getArea();
         
         // Print the area of the object.
         Console.WriteLine("The area is: {0}",  sq.getArea());
         Console.WriteLine("The cost is: {0}" , sq.getCost(area));
         Console.ReadKey();
      }
   }
}

Output:

The area is: 25
The cost is: 250