In Focus

Inheritance With Example in C#

In this article, let's see how to implement 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 C#.


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 is Inheritance

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.

Inheritance Example


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.
Interface Example  

  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:

Inheritance in C%

Further recommended readings on C# inheritance 
Here are more articles on inheritance and object oriented programming in C#.
Types of Inheritance in C# 
In this article, we will learn about C# inheritance and types of inheritance in C# and .NET with code examples.

Overview of Inheritance in C#
In this article, you will learn about inheritance in C#. Inheritance is one of the major pillars of object-oriented programming. Here is an overview of inheritance.

Inheritance with Example in C#
Here are some example of inheritance using C#.
Learn all about Inheritance in C#
In this article, you will learn about Inheritance in C#.
Playing with inheritance in C#
This article explains the various behaviors of inheritance in various cases.
Inheritance examples in C#
This article explains inheritance with examples in C#.
Multiple Inheritance in C#
Can you inherit from multiple classes in C#? Simply put, this cannot be done. However there are ways around it. From a design perspective you must ask yourself, will a Class fully represent an object?
Multiple Inheritance in C# using Interfaces
This is the simple mathematical operation program demonstrating how multiple inheritance can be achieved in C# using Interface Concept.
Inheritance in C#
Interface inheritance defines a new interface in terms of one or more existing interfaces. Implementation inheritance defines a new implementation in terms of one or more existing implementations.