Inheritance with Example in C#

Inheritance is one of the key pillars of OOP and C#, a modern object-oriented programming language. If you are new to object-oriented programming, please read Object Oriented Programming Concepts in C#. In this article, you will learn how to implement inheritance in C# with code examples. 

Introduction

Real-world Example

A scientific calculator is an extended form of a calculator. Here, the calculator is the parent, and the scientific calculator is the child object. The child object inherits the key properties of the base object. For example, the calculator has common functions such as adding, subtracting, multiplying, and dividing. But the scientific calculator has some advanced functions such as power and others. 

Programming Example

The process of acquiring the existing functionality of the parent and the newly 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#, an inherited class 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 unique elements.

Inheritance Example

Diagram

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  

Complete code example in C#

//Base class or Parent class.  
class Shape  
{  
    public double Width;  
    public double Height;  
    public void ShowDim()  
    {  
        Console.WriteLine("Width and height are " +  
        Width + " and " + Height);  
    }  
}  
// Triangle is derived from Shape.  
//Drived class or Child class.  
class Triangle : Shape  
{  
    public string Style; // style of triangle  
    // Return area of triangle.  
    public double Area()  
    {  
        return Width * Height / 2;  
    }  
    // Display a triangle's style.  
    public void ShowStyle()  
    {  
        Console.WriteLine("Triangle is " + Style);  
    }  
}  
//Driver class which runs the program.  
class Driver  
{  
    static void Main()  
    {  
        Triangle t1 new Triangle();  
        Triangle t2 new Triangle();  
        t1.Width =4.0;  
        t1.Height =4.0;  
        t1.Style ="isosceles";  
        t2.Width =8.0;  
        t2.Height =12.0;  
        t2.Style ="right";  
        Console.WriteLine("Info for t1: ");  
        t1.ShowStyle();  
        t1.ShowDim();  
        Console.WriteLine("Area is " + t1.Area());  
        Console.WriteLine();  
        Console.WriteLine("Info for t2: ");  
        t2.ShowStyle();  
        t2.ShowDim();  
        Console.WriteLine("Area is " + t2.Area());  
    }  
}  

The output from this program is shown here:

Inheritance in C%

There are multiple types of inheritance in C#. Read Types of Inheritance in C# to learn more.

Similar Articles