C# – Inheritance

unless this is your time at object oriented programming , you will have heard a lot about Reusability and Extensibility. Reusability is the property of a module ( a component , class or even a method ) that enables it to be used in different applications without any or little change in its source code. Extensibility of a module’s is it’s potential to be extended ( enhanced ) as new needs evolve. Reusability in object oriented programming languages is achieved by reducing coupling between different classes, while extensibility is achieved by sub- classing . The process of sub- classing a class to extend its functionality is called Inheritance or sub – typing.

The original class ( or the class that is sub – typed ) is called the base, parent or super class. The class that inherits the functionality of the base class and extends it in its own way is called the sub, child, derived or inherited class.

Base class

Derived Class Derived Class Derived Class

Before we go on to implementation , here are some key – points about inheritance in C#.

  1. C# , like Java and Contrary to C++, allows only single class inheritance. Multiple inheritance of classes is not allowed in C#.
  2. The Object class defined in the System namespace is implicit the ultimate base class of all the classes in C# ( and the .NET framework)
  3. Interfaces in C# can inherit more than one interface.

So, multiple inheritance of interfaces is allowed in C# (again similar to Java). We will look at interfaces in detail in the coming lessons. Structure (struct) in C# can only inherit ( or implement ) interfaces and can not be inherited.

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.

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> {

. . .


Consider a base class shape and its derived class Rectangle –

using system;

namespace InheritanceApplication {

class Shape {

public void set width ( int w ) {

width = w;


public void SetHeight ( int h ) {

height = h;


protected int width;

protected int height;


// Derived class

class Rectangle : Shape {

public int getArea ( ) {

return (Width * height);



class RectangleTester {

static Void Main (string [ ] args ) {

Rectangle Rect = new Rectangle ( );

Rect. setWidth (5);

Rect. setHeight (7);

// print the area of the object.

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

Console.ReadKey ( );




Where the above code is compiled and executed, it produces the following result –

Total area : 35

Leave a Reply

Your email address will not be published. Required fields are marked *