---
title: "Inheritance in C#"  
description: "In this article, I’m trying to explain the concept of inheritance.  One of the most important concepts in object-oriented programming is inheritance."  
author: "Sumit Kesarwani"  
published: 2013-05-13  
updated: 2014-09-18  
canonical: https://www.mindstick.com/blog/502/inheritance-in-c-sharp  
category: "c#"  
tags: ["c#"]  
reading_time: 3 minutes  

---

# Inheritance in C#

In this article, I’m trying to explain the [concept of inheritance](https://www.mindstick.com/forum/158920/explain-the-concept-of-inheritance-in-c-sharp-and-provide-an-example).\

One of the most important concepts in object-[oriented programming](https://www.mindstick.com/forum/162/object-oriented-programming-oop-s) is inheritance. Inheritance allows us to define a class in terms of another class, which makes it easier to create and maintain an application. This also provides an opportunity to reuse the code functionality and fast implementation.

When creating a class, instead of writing completely new [data members](https://www.mindstick.com/interview/2390/can-you-achieve-runtime-polymorphism-by-data-members-in-java) and member functions, the programmer can design that the new class should inherit the members of an existing class. This existing class is called the base class, and the new class is [referred to as](https://answers.mindstick.com/qa/39930/at-the-turn-of-the-century-widespread-panic-occurred-that-at-midnight-all-computers-would-reset-to-zero-and-life-as-we-knew-it-would-cease-to-exist-what-was-this-doomsday-theory-commonly-referred-to-as) the derived class.

##### Syntax:

```
[Access Modifier] class ClassName : baseclassname{}
```

##### Inheritance can be classified to 5 types:

##### 1. Single Inheritance

##### 2. Hierarchical Inheritance

##### 3. Multi Level Inheritance

##### 4. Hybrid Inheritance

##### 5. Multiple Inheritance

##### \

##### Single Inheritance

when a single [derived class](https://answers.mindstick.com/qa/30686/what-is-a-base-class-and-derived-class) is created from a single [base class](https://www.mindstick.com/blog/116/base-class-initilization) then the inheritance is [called as](https://www.mindstick.com/forum/471/why-is-white-box-testing-called-as-glass-box-testing) single inheritance.

##### Hierarchical Inheritance

when more than one derived class is created from a single base class, then that inheritance is called as hierarchical inheritance.

##### Multi Level Inheritance

when a derived class is created from another derived class, then that inheritance is called as multi level inheritance.

##### Hybrid Inheritance

Any combination of single, hierarchical and multi level inheritances is called as hybrid inheritance.

##### Multiple Inheritance

when a derived class is created from more than one base class then that inheritance is called as [multiple inheritance](https://www.mindstick.com/forum/159471/how-does-c-plus-plus-support-multiple-inheritance-and-what-are-potential-issues-associated-with-it). But multiple inheritances are not supported by .net using classes and can be done using interfaces.

##### Example

```
using System;namespace InheritenceConsoleApplication{    class BaseClass    {        /// <summary>        /// create a base class and a method in it        /// </summary>        /// <param name="x"></param>        /// <param name="y"></param>        public void add(int x, int y)        {            Console.WriteLine("Addition : " +(x+y));        }    }    class DerivedClass : BaseClass //inherit base class    {        /// <summary>        /// create a derived class and a method in it        /// </summary>        /// <param name="x"></param>        /// <param name="y"></param>        public void multiply(int x, int y)        {            Console.WriteLine("Multiply ; "+(x*y));        }    }    class Program    {        static void Main(string[] args)        {            DerivedClass dc = new DerivedClass(); // create a derived class object            dc.add(21,34); // call base class method with derived class object            dc.multiply(4,5);        }    }}
```

##### Output :

Addition : 55

Multiply : 20

In this example, I have created two classes – one base class with method add() and derived class with method multiply(), derived class inherits the base class , in class program – create the derived class object and call the base [class method](https://www.mindstick.com/interview/22908/what-are-java-string-class-method) add() and derived class method multiply() with it and see the desired output.

---

Original Source: https://www.mindstick.com/blog/502/inheritance-in-c-sharp

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
