What is Inheritance, Overriding,OverLoading ?

Inheritance is the process of creating a new class from one or more existing class.  In otherwords, a new class acquires the qualities of existing class.

Example.  Lets say that we need to model a bank account.  Lets say that the bank has only two types of account  – Savings Account and Current Account.  Both of these accounts have some charateristics in common.  So, we define a class based on common charateristics called BankAccount.   Now to create a class called for SavingsAccount / CurrentAccount, we can derive/inherit the fetaures of BankAccount. This is known as inhertitance.   BankAccount is called base class/ super class.  SavingsAccount/CurrentAccount is called as derived class / sub class

Inheritance can be single (only one base class) or multiple (two or more base class)).   C# supports only single Inheritance.  Multiple  inheritance is not support in C#.  To achieve multiple inheritance in C# we need to use Interfaces

Inheritance in C# is indicated by the following syntax

<access modifier><classname> : <baseClassName>

Example : public class SavingsAccount : BankAccount { // declare the additional info needed for savings account}

In Inheritance, what ever is true for BaseClass is also true for derived class.  For example, all the attributes and member functions in baseclass  – BankAccount  is also true for derived class -SavingsAccount .

Since derived classes are special category of base class, its called as specialization.  In otherwords, specialization is the process of defining a new class based on definition of existing class.  Generalization  is the term used to refer the baseclass (ie recognizing the common features of several existing classes and creating a common base class for all of them) . 

Inheritacne is based on accessibility of the baseclass.  All public members are inherited to derived class. But there is some complexities in inheriting other accessibility types.  We will discuss them later. 

Inheritance is also called as relationship between classes.  It is different from that of Aggregation and Association.  It differs at the object level.  While Association and aggregation is between individual objects,  Inheritance on the other hand is the way of describing a single object.  ie with Inheritance, an object is simultaneously an instance of derived class and all of its base classe(s).

Note : In C# System.Object class serves as the base class for both user-defined and in-built datatypes.  We will discuss about Object class in seperate post.

The single most important advantage of Inheritance is Reusability. There are many other advantages like creating a new derived class as and when required with out affecting the base class (which will be of great benifit in software development where requirements keepi changing every now and then).

If we want the derived class to have a different implementation of a method than that of base class, then we use a concept called as Overriding.  In otherwords customizing the way that a derived class performs a service—that is, how it responds to a given message—as compared with the way that its base class would have responded to the same message, is accomplished via a technique known as overriding

Overriding  means changing the methods implementation in derived class without changing the interface or method signature.  In order to override a method in C#, the method to be overridden has to be declared virtual  in the base class using the virtual keyword.  Declaring a method a virtual means, it may be (but its not necessary) overriden in the derived class.   The derived class has to then override the method using override keyword in the derived class method implementation.  The overriddent method in the dervided class should have the same return type and method signature.

public class BankAccount   // this is the base class in our example
 private string firstName;
 private string lastName;
 public virtual string getFullName()
  return  lastName + firstName;

public Class SavingsAccount: BankAccount  // this is the derived class in our example

 public override string getFullName()
  return lastName + ", " + firstNAme;

 Overloading  :  Overloading allows two or more methods in the same class to have a same name with different parameter list.
public void Draw(int x, int y);
public void Draw(int x, int y, int z);

Note : Constructors are overloaded and they are NOT OVERRIDDEN.


Author: azagappan

Hi.. I am Azagu. I live in Chennai, Tamil Nadu, India. I am a .NET developer. You can reach me at azagappan at gmail.com

6 thoughts on “What is Inheritance, Overriding,OverLoading ?”

  1. thanks for ur explaination for C#. It is clear enough, but if can give more example in how C# implement single, multiple, multilevel, and hierarchical inheritance. If can send it to my email address. Thanks.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s