Click here to Skip to main content
11,933,037 members (60,812 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as


16 bookmarked

Runtime Polymorphism - Simplified

, 3 Sep 2014 CPOL
Rate this:
Please Sign up or sign in to vote.
Runtime polymorphism - simplified


Polymorphism is a wide concept which is used across the application for bringing in feature of OOPs.

Polymorphism is of two types, static (compile time) and dynamic polymorphism (run time). We will deal with what is run time polymorphism and its implementation as it is a big and an important concept of OOPs and a widely asked question.


Now before understanding what is runtime polymorphism, let's know about abstract class, it is the blue print of the problem and it contains abstract methods (partial + complete methods).

Assume from the below figure that Employee is an abstract class and we are trying to build an application for College. Therefore, Employee has properties such as Id, Name and Salary Per Day which are fields in Class. While Lab assistant, Lecturer and Admin are the Type of Employee. These type of employee will inherit all the details of abstract Class Employee, therefore Id, Name and salary per day are properties of Lab assistant, Lecturer and Admin.

We have other methods like Display() and Cal_Salary() which will perform operations on Id, Name and salary of each employee; which will either display or calculate details.


  • Display(): This method is treated as Virtual method
  • Cal_Salary(): This method is Abstract method, which means either implementation can be partial or complete. Here, calculation for each employee varies based on salary structure. Therefore, it's just a blue print, not complete implementation for employee. We can implement the method and write further implementation for each employee.

Using the Code

Now let's try to implement the code based on the above interpretation.

Below are the details about Employee class in code form:

Now, let's create Employees: Lab Assistant class.

In the below class, we have used base keyword to initialize the base class variables in base class itself. While LabNo is initialized here in Lab assistant constructor, display() is directly overridden as there is no functionality and helps in displaying initialized field in derived class (i.e.) LabNo.

Now how to evaluate the details of Lab assistant:

Just create a reference for employee class and pass it as object to LabAssistant class.

On press, F5 below is the OUTPUT;

Therefore, it has displayed all the LabAssistant details with gross salary details.

Similarly, we can create for Lecturer and Admin.

For Lecturer, LabNo is replayed by Subject and HRA is 20% of basic salary.

The code is as below:

For Admin, there is no property other than base class properties and HRA is 15% of basic salary. Therefore, the code goes as below:

We will give choice to the end user to select the employee type and storing choice in variable "ch" as in below code:

When user enters their choice, we will create object for type of employee on the fly as in the below code:

Further, we don't know which method is going to be called at compile time. It's completely end user choice dependent (i.e.) if User enters.

Choice 1 will give details of lab assistant.

Choice 2 its lecturer.

Choice 3 its Admin.

These details will be known at runtime and reference for employee will be created and assigned as object to respective choice given by user. This is the basic concept of run time polymorphism through implementation.

Live example we use is Microsoft Paint where a class is reference of class diagram and if we click drag and drop; it is going to select the area, because our reference becomes select area.

Now, we’ll make it an object of circle and we’ll execute the same method click drag and drop and try to draw a circle.

Because now our object reference is circle, if we select on rectangle, now our object reference becomes rectangle. And performing the same method click drag drop.

From the above, we can draw a conclusion that object is created at runtime based on what we select like line or magnifier, etc. Therefore, Microsoft Paint is working on runtime polymorphism. We see that we are calling single method, but it's calling various tasks depending upon the object that it makes at runtime. This is nothing but Runtime Polymorphism real-time example.

Point of Interest

  • Runtime polymorphism is, if we have an abstract class and n number of classes which are implementing the abstract class and we’re making the reference of abstract class and making it an object of any class at runtime and calling the methods.
  • The method's code is referenced at runtime depending upon the users' input.
  • Runtime in the sense at the time of execution.
  • Polymorphism means many meanings and calculate salary has many meanings. It calculates the salary of admin, lectures and lab assistant, but at runtime.
  • At compile time, we cannot say what method is going to execute.
  • So this is all about runtime polymorphism.

The Code

using System;

    abstract class Employee
     protected int EmpId;
     protected string EmpName;
     protected double SalPerDay;

     public Employee(int EmpId, string EmpName, double SalPerDay)
      this.EmpId = EmpId;
      this.EmpName = EmpName;
      this.SalPerDay = SalPerDay;

     public virtual void Display()
      Console.WriteLine("EmpId : " + EmpId);
      Console.WriteLine("EmpName : " + EmpName);
      Console.WriteLine("Emp Sal Per Day : " + SalPerDay);

     public abstract void CalculateSalary(int days);

    classLab Assistant : Employee
     protected int LabNo;
     public LabAssistant(int EmpId, string EmpName, double SalPerDay,int LabNo):base(EmpId,EmpName,SalPerDay)
      this.LabNo = LabNo;

     public override void Display()
      Console.WriteLine("LabNo :" + LabNo);

     public override void CalculateSalary(int days)
      double GS = SalPerDay * days;
      Console.WriteLine("Gross Salary is :"+GS);

   class Lecturer : Employee
    protected string Sub;
    public Lecturer(int EmpId, string EmpName, double SalPerDay, string Sub)
            : base(EmpId, EmpName, SalPerDay)
     this.Sub = Sub;

    public override void Display()
     Console.WriteLine("Sub :" + Sub);

    public override void CalculateSalary(int days)
     double GS = (SalPerDay * days)+((SalPerDay * days)*20/100);
     Console.WriteLine("Gross Salary is :" + GS);

   class Admin : Employee
    public Admin(int EmpId, string EmpName, double SalPerDay) : base(EmpId, EmpName, SalPerDay)

    public override void CalculateSalary(int days)
     double GS = (SalPerDay * days) + ((SalPerDay * days) * 15 / 100);
     Console.WriteLine("Gross Salary is :" + GS);

   class Program
    static void Main(string[] args)
     Employee E;
     E = newLabAssistant(123, "Peter", 67.7, 34);
     Console.WriteLine("Enter You Choice");
     Console.WriteLine("1.LabAssistant 2.Lecturer 3.Admin 4.Exit");
     int ch = int.Parse(Console.ReadLine());

     switch (ch)
      case 1:
      E = newLabAssistant(123, "Peter", 67.7, 34);
      case 2:
      E = newLecturer(124, "Tom", 89.6, "MS.Net");
      case 3:
      E = newAdmin(126, "Lilly", 45.8);

      case 4:





This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Mohd Manzoor Ahmed
India India
Manzoor is a Microsoft Certified Trainer who has been working on MS .Net technologies for more than a decade. Apart from development he is also passionate about delivering training on various MS .Net technologies and he has 10+ years of experience as a software development teacher. He writes articles for code-project as well. His YouTube channel has 1 million hits. He is the founder of ManzoorTheTrainer portal.

"I focus on simplifying, complex concepts..." - ManzoorTheTrainer

Founder of [Free .net video tutorials on MS SQL Server, Asp.Net, C#.Net, Ado.Net, Entity Framework, MVC, Web Services, Android]

You may also be interested in...

Comments and Discussions

GeneralMy vote of 5 Pin
Renju Vinod15-Oct-14 21:20
professionalRenju Vinod15-Oct-14 21:20 
GeneralRe: My vote of 5 Pin
Mohd Manzoor Ahmed15-Oct-14 21:26
memberMohd Manzoor Ahmed15-Oct-14 21:26 
QuestionSome thoughts Pin
Nelek10-Oct-14 4:48
memberNelek10-Oct-14 4:48 
AnswerRe: Some thoughts Pin
Mohd Manzoor Ahmed11-Oct-14 8:40
memberMohd Manzoor Ahmed11-Oct-14 8:40 
GeneralRe: Some thoughts Pin
Nelek11-Oct-14 9:07
memberNelek11-Oct-14 9:07 
QuestionGood Example Pin
Anil Srivastava14-Sep-14 22:21
memberAnil Srivastava14-Sep-14 22:21 
AnswerRe: Good Example Pin
Mohd Manzoor Ahmed15-Sep-14 16:41
memberMohd Manzoor Ahmed15-Sep-14 16:41 
GeneralYap simple it is... Pin
Rehman Ahmad Ch4-Sep-14 7:23
memberRehman Ahmad Ch4-Sep-14 7:23 
GeneralRe: Yap simple it is... Pin
Mohd Manzoor Ahmed4-Sep-14 7:31
memberMohd Manzoor Ahmed4-Sep-14 7:31 
GeneralMy vote of 5 Pin
Humayun Kabir Mamun3-Sep-14 22:11
memberHumayun Kabir Mamun3-Sep-14 22:11 
GeneralRe: My vote of 5 Pin
Mohd Manzoor Ahmed4-Sep-14 7:30
memberMohd Manzoor Ahmed4-Sep-14 7:30 
GeneralMy vote of 5 Pin
Dreamcoder7863-Sep-14 22:04
memberDreamcoder7863-Sep-14 22:04 
GeneralRe: My vote of 5 Pin
Mohd Manzoor Ahmed4-Sep-14 7:29
memberMohd Manzoor Ahmed4-Sep-14 7:29 
GeneralSimplified... really? Pin
Paulo Zemek3-Sep-14 10:05
professionalPaulo Zemek3-Sep-14 10:05 
GeneralRe: Simplified... really? Pin
Mohd Manzoor Ahmed4-Sep-14 0:15
memberMohd Manzoor Ahmed4-Sep-14 0:15 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.151126.1 | Last Updated 3 Sep 2014
Article Copyright 2014 by Mohd Manzoor Ahmed
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid