---
title: "Inheritance in c#"  
description: "In this blog I am intended to explore the concept of Inheritance in OOPs and C#.  Inheritance is the basic concept of OOPs (object oriented programmin"  
author: "shreesh chandra shukla"  
published: 2013-07-15  
updated: 2014-09-18  
canonical: https://www.mindstick.com/blog/552/inheritance-in-c-sharp  
category: "c#"  
tags: ["c#"]  
reading_time: 5 minutes  

---

# Inheritance in c#

In this blog I am intended to explore the [concept of Inheritance](https://www.mindstick.com/forum/158920/explain-the-concept-of-inheritance-in-c-sharp-and-provide-an-example) in OOPs and C#.\

Inheritance is the [basic concept](https://www.mindstick.com/interview/23039/can-you-tell-me-about-basic-concept-of-boxing-and-unboxing) of OOPs (object [oriented programming](https://www.mindstick.com/forum/162/object-oriented-programming-oop-s)). This assists the programmer to [code reusability](https://www.mindstick.com/articles/337478/the-role-of-design-patterns-in-enhancing-code-reusability) of existing code.

Inheritance implement by inheriting the base (super/parent)class member to its derived (sub/child)class, which in turn enables the [base class](https://www.mindstick.com/blog/116/base-class-initilization) to access the code of its base class. That means we extend our [derived class](https://answers.mindstick.com/qa/30686/what-is-a-base-class-and-derived-class) functionality by inheriting the base class.

Every [programming languages](https://www.mindstick.com/articles/12386/5-up-and-coming-programming-languages-to-know-about) that supports OOP must contains a provision for implementing Inheritance. And each language has its own syntax to create and use it.

Basically there are two types of inheritance in OOPs. But both types of inheritance is rarely supported by language(c++) because of problem in second type of inheritance ([multiple inheritance](https://www.mindstick.com/forum/159471/how-does-c-plus-plus-support-multiple-inheritance-and-what-are-potential-issues-associated-with-it))

1. Single Inheritance

2. Multiple inheritance( c# does not supports )

Now we discussed about single inheritance, because many popular OOPs supporting languages (java, c# etc.) does not support the Multiple inheritance. So now we let’s focus on c#(c-Sharp) inheritance [implementation](https://www.mindstick.com/forum/33764/how-to-implementation-of-class-in-c-sharp) issues:

When a derived class inherits from a base class, it gains all the methods, fields, properties and events of the base class. To change the data and behavior of a base class, you have two choices: you can replace the base member with a new derived member, or you can override a virtual base member.

C# supports single inheritance and implemented in following manner

##### Syntax:

<Derived class> : <base class> // the only one base class is place here, because of single inheritance supports.

##### Example:

```
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;
namespace inheritanceExample{    class Program    {        static void Main(string[] args)        {            Rectangle rect = new Rectangle(10, 15);//create object of rectangle class
            rect.Area();//call method area() of its own class            Square sqr = new Square(8);//create object of square class
            sqr.Area();//call method area() of its own class            Console.ReadKey();        }
    }    // base class shape    class Shape    {        public int length;        public int breadth;        public Shape(int length)        {            this.length = length;
        }        public Shape(int length, int breadth)        {            this.length = length;            this.breadth = breadth;
        }        public void Area()// base class method area()        {            Console.WriteLine("Area method of base class");
        }    }    //derived class square inherite base class shape    class Square : Shape    {        public Square(int length)//calling base class constructor            : base(length)        {
        }// creating/overriding new definition of area method of base class        public new void Area()       {            base.Area();//calling area() method of base class            Console.WriteLine("Area of Square(L x L) = {0} x {0} = {1}",                length,                length * length);        }
    }
    //derived class rectangle inherite base class shape    class Rectangle : Shape    {        public Rectangle(int length, int breadth)//calling base class constructor
            : base(length, breadth)        {
        }
// creating/overriding new definition of area method of base class
        public new void Area()        {            base.Area();//calling area() method of base class            Console.WriteLine("Area of Rectangle(L x B) = {0} x {1} = {2}",                length,                breadth,                length * breadth);        }
    }
}
```

As you have been noticed, the derived class never contains

\

public int length;

public int breadth;

\

Then how its methods are able to access the length and breadth in its

\

method calls like

\

```
public new void Area()        {
            base.Area();//calling area() method of base class            Console.WriteLine("Area of Rectangle(L x B) = {0} x {1} = {2}",                length,                breadth,                length * breadth);
        }
```

\

How surprise!

No, this is possible due to the inheritance used in this program, which causes the derived classes Rectangle and Triangle inherits all the member of base class Shape , which already contains (length and breadth). So after inheriting Shape class the derived classes can also have (length and breadth). Thus no need to define it again in derived class separately. Only needs to use it, instead of redefining it.

---

Original Source: https://www.mindstick.com/blog/552/inheritance-in-c-sharp

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
