---
title: "What is  Inheritance in C#"  
description: "What is  Inheritance in C#"  
author: "Simond Gear"  
published: 2016-05-04  
updated: 2020-09-20  
canonical: https://www.mindstick.com/interview/23014/what-is-inheritance-in-c-sharp  
category: "c#"  
tags: ["c#", "oops", "inheritance"]  
reading_time: 3 minutes  

---

# What is  Inheritance in C#

**Inheritance:**

\

Inheritance is the ability to define a new class or object that inherits the behavior and its functionality of an existing class. The new class or object is called a child or subclass or derived class while the original class is called parent or base class.

\
In other words when a class acquire the property of another class is known as inheritance.We can say that, inheritance is the second pillar of OOPs because with the help of single class we can’t make our project. Through inheritance we can achieve **code reusability and encapsulation**.

## For example, I have created a Shape class as a Parent class

```
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace OopsConcepts{    class Shape    {        protected int width;        protected int height;             public void setWidth(int w)        {            width = w;        }        public void setHeight(int h)        {            height = h;        }        }}
```

## \
Here I have creating an another class Rectangle that inherit to Shape class

```
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks; namespace OopsConcepts{    class Rectangle:Shape    {        public int getArea()        {            return(width * height);        }        public int getPerimeter()        {            return2*(width + height);        }          }} 
```

\

And create another entry point class (RectangleTest) and which we are creating

\

object of the Rectangle class for calling the method of their respective class.

\

```
using System;                                                                                   using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace OoopsConcepts{    class RectangleTest    {        static void Main(string[] args)        {            // Inhertance Test                       Rectangle rectange = new Rectangle();          
            rectangle.setWidth(10);            rectangle.setHeight(5);            Console.WriteLine("Total area : {0}", rectangle.getArea());            Console.WriteLine("Perimeter : {0}", rectangle.getPerimeter());            Console.ReadLine();}}}
```

**Output:**

Total area: 50

Perimeter: 30

## Answers

### Answer by Simond Gear

**Inheritance:**

\

Inheritance is the ability to define a new class or object that inherits the behavior and its functionality of an existing class. The new class or object is called a child or subclass or derived class while the original class is called parent or base class.

\
In other words when a class acquire the property of another class is known as inheritance.We can say that, inheritance is the second pillar of OOPs because with the help of single class we can’t make our project. Through inheritance we can achieve **code reusability and encapsulation**.

## For example, I have created a Shape class as a Parent class

```
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace OopsConcepts{    class Shape    {        protected int width;        protected int height;             public void setWidth(int w)        {            width = w;        }        public void setHeight(int h)        {            height = h;        }        }}
```

## \
Here I have creating an another class Rectangle that inherit to Shape class

```
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks; namespace OopsConcepts{    class Rectangle:Shape    {        public int getArea()        {            return(width * height);        }        public int getPerimeter()        {            return2*(width + height);        }          }} 
```

\

And create another entry point class (RectangleTest) and which we are creating

\

object of the Rectangle class for calling the method of their respective class.

\

```
using System;                                                                                   using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace OoopsConcepts{    class RectangleTest    {        static void Main(string[] args)        {            // Inhertance Test                       Rectangle rectange = new Rectangle();          
            rectangle.setWidth(10);            rectangle.setHeight(5);            Console.WriteLine("Total area : {0}", rectangle.getArea());            Console.WriteLine("Perimeter : {0}", rectangle.getPerimeter());            Console.ReadLine();}}}
```

**Output:**

Total area: 50

Perimeter: 30


---

Original Source: https://www.mindstick.com/interview/23014/what-is-inheritance-in-c-sharp

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
