Inheritance is a very important aspect of Object Oriented Programming. It allows a class to inherit all the properties of another class. Let’s discuss this topic in detail.

What is Inheritance?

Inheritance is one of the most important pillars of Object Oriented Programming. Inheritance allows a class to access all the property of the Base class and also Base class can have its own behaviour. Using Inheritance a class can be act as a template for other classes. A class that allows inheritance is called as Base Class and the class that inherits other class is called as derived class.

Real World Example of Inheritance:

An example of Inheritance that exists in the real world is Shape class, which is a base class and its derived class can be Triangle, Rectangle and Square.

The basic concept of Inheritance is around IS-A relationship. For Example, Triangle is a Shape.

Inheritance

When should we use Inheritance?

I think we got some basic knowledge of Inheritance with an example. Now the point is when we should use Inheritance. As I told you the idea of Inheritance is around IS-A relationship. There are two types of relationships between classes:

  • IS-A Relationship.
  • HAS-A Relationship

Inheritance is used when your classes have IS-A Relationship. Such as Triangle IS-A Shape. A Rectangle is a Shape. Suppose I have a class that calculates the area of the shape then the relationship between the Shape and ShapeArea will be HAS-A.

Inheritance

Why should we use Inheritance:

Inheritance is a way to achieve the functionality of one class without creating a new class. It reduces the complexity of the code and eliminates the repetitive code. With Inheritance, it becomes to maintain our code.

An example of Inheritance in C#:

Open Visual Studio and Create a Console Application Project.

Inheritance

Open Program.cs file and write the following code.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace InheritanceDemo
{
class Program
{
class Shape
{
public double dSideA;
public double dSideB;

public void ShapeDimensions()
{
Console.WriteLine("Dimensions of the shape are " +
dSideA + " and " + dSideB);
}
}
// Triangle is derived from Shape. Both have IS-A relationship
 
class Triangle : Shape
{
 
public void Area()
{
Console.WriteLine("Area of the Triangle Is :" + dSideA * dSideB / 2);
 
}
 
 
}
class Rectangle : Shape
{
public void Area()
{
Console.WriteLine("Area of the Rectagle Is :" + dSideA * dSideB);

}
}
static void Main(string[] args)
{
Triangle objTriangle = new Triangle();
objTriangle.dSideA = 10;
objTriangle.dSideB = 20;
objTriangle.ShapeDimensions();
objTriangle.Area();
Rectangle objRectangle = new Rectangle();
objRectangle.dSideA = 40;
objRectangle.dSideB = 50;
objRectangle.ShapeDimensions();
objRectangle.Area();
Console.ReadKey();

}
}
}

Run the application and look over the output window.

Inheritance

You can download the complete source code for the application from here.

Hope you understand the concept of Inheritance in C#. We will create more example on Inheritance in later sessions.

View More:

Thank You.

Leave a Comment