Overview Of Inheritance In C#

Introduction

  • Inheritance is one of the major pillars of object-oriented programming.
  • Inheritance allows the creation of hierarchical classification.
  • Inheritance allows the code reuse and by code reuse, we can reduce time and errors.
  • Inheritance allows us to extend the software.
  • Inheritance is similar to a parent-child relationship -- each child has an ‘is-a’ relationship with its parent. For example, an apple ‘is-a’ fruit. Green ‘is-a’ color.
Definition

Inheritance is a mechanism by which a new class is derived from an existing one. The new class, which is being created is called as a derived class and the class from which it is derived is called a base class. With the help of inheritance; a hierarchy of the classes can be created.

Features of inheritance
  • Object-oriented programming extends the abstract data types to allow for/subtype relationships. It is achieved through an inheritance.
  • The inheritance means to receive the properties of an already existing class.
  • When an existing class inherits, the derived class inherits all the data members as well as the member methods from the base class. but not all of them can be accessed by the member methods of the derived class. The accessibility of the base class members in the derived class depends on their access specifiers.
Explanation

Let’s take an example of different types of employee relationships, as shown below.



This is ‘is-a’ kind of hierarchy. More than one class can inherit the attributes from a single base class.

Here, an Employee is the base class, since it is the most general category of the employees and has all the common attributes of all employees like Employee Name, Age etc.

Manager, Developer and SalesPerson are the derived classes, since these classes can derive from Employee class.

A derived class can be used as a base class to derive other classes. For example, another class SalesManager can be derived from Manager class.

Note

Multiple inheritance is not possible in C#, but multi level inheritance is possible. As described above, SalesManager can be derived from Manager class and Manager class can be derived from an employee class but SalesManager cannot derive from both Manager and SalesPerson.

Example of an inheritance


In the example given above, you can see that there are two classes Employee and Manager. Here, Manager class is inheriting from Employee class. As you can see in the example given above in Blue rectangle i.e. how you have to inherit the class with a colon.

Now, see in another class called Program, where we have our main method. I have created an instance of Manager class object and you can see here, which methods are available in the Manager class; i.e., CalculateIncentives is present in the same class and calculates salary, which is present in base class of an Employee.

Accessibility of a base class members in derived class depends on the type of modifier used to define them.

Access Specifier in Base ClassAccessibility Within AssemblyAccessibility Outside Assembly
PrivateNo
No
PublicYes
Yes
ProtectedYes*Yes*
InternalYes
No
Protected internalYes
Yes*

*--accessibility in derived class only

All the members of the base class are inherited by the derived class. The accessibility of these members depends on their access specifier.

  • private members are not accessible even by the immediate derived class within or outside the assembly.
  • Other access specifiers like public, protected or protected internal are accessible to the immediate derived classes within or outside the assembly.
  • internal is not accessible outside the assembly.
  • public members of the base class can be accessed, using the object of the derived class.

When a class is derived from another class, the member initializes the list, which is the best way to initialize the base class data members. Member initialization of the base class is achieved in the derived class constructor, using ‘base’ keyword, which is used to call any method of the base class from the derived class.

Constructor of the derived class first invokes the constructor of the base class. If ean explicit call to a parameterized constructor is not given, it calls the base class no-argument constructor. Now, the code inside the derived class constructor gets executed.

Base class constructor should be explicitly called, using ‘base’ keyword, else as per the order of constructor call (base to derived), default constructor is called, which is shown below.


Note

You can use base keyword to access the base class members to access in the derived class.

Shadowing

A base class defines a data member, a method or a property. As explained above, these members are inherited by the derived class. If a derived class needs to hide one of these members, shadowing concept is used.

In C#, new modifier is used in the derived class to hide the base class implementation.

The new keyword can be applied to any member type inherited from the base class (field, constant, static members, property etc).

For example,

I have created a function in Manager (derived) class, which has same function name and signature as Employee (base) class. As you can see in the figure given below, when I try to create a function in the derived class, the function with same signature is present in the base class and I got a warning, which says that if you create this function, it will hide the base class function. If you want to do it, then use a new keyword in the function


After adding new keyword in the function, it will be displayed, as shown below.


You can call this function in the main function, as shown below. 

  1. class Program  
  2.     {  
  3.         static void Main(string[] args)  
  4.         {  
  5.             Manager mgr = new Manager();  
  6.             double sal = mgr.CalculateSal();  
  7.             Console.WriteLine("Salary = "+sal);  
  8.             Console.ReadKey();  
  9.         }  
  10.     }   

Now, you can see the result, which is calculating from the derived class function. It means the derived function is hiding the base class function.