Inheritance in C# (Part – 16)

The article explains below concepts:

  • What is Inheritance ?
  • Why Inheritance?
  • Advanatages of Inheritance?
  • Syntax of Inheritance
  • Example of Inheritance

What is Inheritance ?

Inheritance enables you to create new classes that reuse, extend, and modify the behavior  defined in other classes.

The class whose members are inherited is called the base class, and the class that inherits those members is called the derived class.

A derived class can have only one direct base class

Why Inheritance?

We will take example of 2 classes and then rewrite it using Inheritance.

So in both the classes, there are lot of duplicate codes. so we can remove those duplicates by using Inheritance.

public class PermanentEmployee : Employee
float YearlySalary;

So in this example, we have created a common class called Employee with all common code and that is called base class.Also we have created PermanentEmployee and Contractor two derived class.

So in base class you will specify all the common fields,properties,methods in base class, which allows reusability. In the derived class you will have fields,properties and methods that are specific to them.

Advantages of Inheritance:

  • Inheritance allows code reuse.
  • Code reuse can reduce time and error


DerivedClass inherits from BaseClass.C# only supports single class and multiple interface inheritance.Derived class is a specialization of BaseClass. Base classes are instantiated automatically before derived class.

© 2015, www.techkatak.com. All rights reserved.