Inheritance With Example in C#

Today in this article I will show you OOP concepts in the C# language.

I have updated this article on my personal blog. you can read it Inheritance With Example in Csharp.


Real-world Example

A scientific calculator is an extended form of a calculator. Here calculator is the parent and scientific calculator is child object.

Programming Example

The process of acquiring the existing functionality of the parent and with the new added features and functionality of a child object.

What Inheritance is

Inheritance is one of the three foundational principles of Object-Oriented Programming (OOP) because it allows the creation of hierarchical classifications. Using inheritance you can create a general class that defines traits common to a set of related items. This class can then be inherited by other, more specific classes, each adding those things that are unique to it.

In the language of C#, a class that is inherited is called a base class. The class that does the inheriting is called the derived class. Therefore a derived class is a specialized version of a base class. It inherits all of the variables, methods, properties, and indexers defined by the base class and adds its own unique elements.



The following diagram shows the inheritance of a shape class. Here the base class is Shape and the other classes are its child classes. In the following program we will explain the inheritance of the Triangle class from the Shape class.
Show Interface  

  1. //Base class or Parent class.  
  2. class Shape  
  3. {  
  4.     public double Width;  
  5.     public double Height;  
  6.     public void ShowDim()  
  7.     {  
  8.         Console.WriteLine("Width and height are " +  
  9.         Width + " and " + Height);  
  10.     }  
  11. }  
  12. // Triangle is derived from Shape.  
  13. //Drived class or Child class.  
  14. class Triangle : Shape  
  15. {  
  16.     public string Style; // style of triangle  
  17.     // Return area of triangle.  
  18.     public double Area()  
  19.     {  
  20.         return Width * Height / 2;  
  21.     }  
  22.     // Display a triangle's style.  
  23.     public void ShowStyle()  
  24.     {  
  25.         Console.WriteLine("Triangle is " + Style);  
  26.     }  
  27. }  
  28. //Driver class which runs the program.  
  29. class Driver  
  30. {  
  31.     static void Main()  
  32.     {  
  33.         Triangle t1 new Triangle();  
  34.         Triangle t2 new Triangle();  
  35.         t1.Width =4.0;  
  36.         t1.Height =4.0;  
  37.         t1.Style ="isosceles";  
  38.         t2.Width =8.0;  
  39.         t2.Height =12.0;  
  40.         t2.Style ="right";  
  41.         Console.WriteLine("Info for t1: ");  
  42.         t1.ShowStyle();  
  43.         t1.ShowDim();  
  44.         Console.WriteLine("Area is " + t1.Area());  
  45.         Console.WriteLine();  
  46.         Console.WriteLine("Info for t2: ");  
  47.         t2.ShowStyle();  
  48.         t2.ShowDim();  
  49.         Console.WriteLine("Area is " + t2.Area());  
  50.     }  
  51. }  

The output from this program is shown here:

Info for t1:
Triangle is isosceles
Width and height are 4 and 4
Area is 8
Info for t2:
Triangle is right
Width and height are 8 and 12
Area is 48