C# (C Sharp) - Inheritance

One of the most important concepts in object-oriented programming 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 speeds up implementation time. 

When creating a class, instead of writing completely new data members and member functions, the programmer can designate 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 the derived class. 

The idea of inheritance implements the IS-A relationship. For example, mammal IS A animal, dog IS-A mammal hence dog IS-A animal as well, and so on. 

Base and Derived Classes

A class can be derived from more than one class or interface, which means that it can inherit data and functions from multiple base classes or interfaces. 

The syntax used in C# for creating derived classes is as follows − 

<acess-specifier > class <base_class> 


class  <derived_class> : <base_class>   


Multiple Inheritance in C#

C# does not support multiple inheritance. However, you can use interfaces to implement multiple inheritance. The following program demonstrates this − Live Demo 

using System; 

namespace InheritanceApplication 

class Shape 

public void setWidth(int w) 

width = w; 

public void setHeight(int h) 

height = h; 

protected int width; 

protected int height; 

// Base class PaintCost 

public interface PaintCost 

int getCost(int area); 

// Derived class 

class Rectangle : Shape, PaintCost 

public int getArea() 

return (width * height); 

public int getCost(int area)

return area * 70; 

class RectangleTester 

static void Main(string[] args) 

Rectangle Rect = new Rectangle(); 

int area; 



area = Rect.getArea(); /

/ Print the area of the object. 

Console.WriteLine("Total area: {0}", Rect.getArea()); 

Console.WriteLine("Total paint cost: ${0}" , Rect.getCost(area)); 


"c# inheritance multiple classes"

"c# inheritance example"

"c# inheritance constructor"

"types of inheritance in c# sharp"

"hierarchical inheritance in c#"

"single inheritance in c#"