07 June 2014

difference between compile time and run-time polymorphism

In this tutorial post let's understand what is polymorphism and what are the difference between compile time polymorphism and runtime polymorphism.

What is polymorphism

Polymorphism means exhibition in many forms. "Poly" means multiple and "morph" means forms. Combining the two words it becomes the exhibition of multiple forms.

polymorphism is having two types. One is called compile-time polymorphism and other is run time polymorphism.

First let's understand compile time polymorphism

class  Program
{
  static void Main(string[] args)
  {
   // passing 2 values to Add() method 
   // calls 2 parameters method
   long sum = Calculator.Add(1, 2);
   Console.WriteLine(sum);
   // Passing 3 values to Add() method 
   // calls 3 parameters method
   sum = Calculator.Add(1, 2, 3);
   Console.WriteLine(sum);
   Console.Read();
   }
}
public static class Calculator
{
public static long Add(int par1, int par2)
 {
   Console.WriteLine("Two parameter method called");
   long sum = par1+ par2;
   return sum;
 }
 public static long Add(int par1, int par2, int par3)
 {
  Console.WriteLine("Three parameter method called");
  long sum = par1 + par2 + par3;
  return sum;
  }
}

Output of the above function overloading program:

Two parameter Add() method called
3
Three parameter Add() method called
6

In the above Calculator class, we have two methods with the same name Add(). One of the methods is taking in two parameters; whereas the other method is taking in 3 parameters. Depending on how many parameters we are passing to a method, a different method is called. Here the name of the method is same, only the number of parameters is varying. This is called function overloading. So using function overloading we can achieve compile-time polymorphism.

Why its called compile time polymorphism?

Which methods get executed will be determined at the time of compilation only. Hence this is called compile time polymorphism. Because the method invocation is decided at compile time only, it cannot change at runtime based on the context. Hence compile-time polymorphism is also called static polymorphism.

Using Function overloading we can implement compile-time polymorphism.

Now let's understand the run time polymorphism

The best is to go through a code example.

class Program
{
 static void Main(string[] args)
 {
  //doctor variable refers to 
  //instance of base Doctor class
  Doctor doctor = new Doctor();
  doctor.TreatPatient();
  // doctor variable refers to 
  // instance of derived Surgeon class.
  doctor = new Surgeon();
  doctor.TreatPatient();
  Console.Read();
  }
}

public class Doctor
{
public virtual void TreatPatient()
 {
Console.WriteLine("Doctor class: I diagnose 
      patients and prescribe medicine."
);
 }
}

public class Surgeon : Doctor
{
 // Override the behavior of the base 
 // class and customize it.
 public override void TreatPatient()
 {
 Console.WriteLine("Surgeon class:
                   I do surgery."
);
 base.TreatPatient();
 }
}

Output of the above function overriding program:

Doctor class: I diagnose patients and prescribe medicine.
Surgeon class: I do surgery.
Doctor class: I diagnose patients and prescribe medicine.

In above program, Doctor is the base class and Surgeon is derived class. In Doctor class, the method TreatPatient() is defined as virtual. That means, the derived classes can override the behavior of the base class. In our example the derived class, Surgeon is specialized form of Doctor, who can do surgery and hence the TreatPatient() method is overridden.

For Doctor type variable, when the Doctor class instance [new Doctor()] is assigned and on invoking the TreatPatient() method, the Doctor class TreatPatinent() method gets executed.

Doctor class: I diagnose patients and prescribe medicine.

In the next line for the same doctor variable, instance of the derived class Surgeon [using new Surgeon()] is assigned and on invoking the TreatPatient() method, the Surgeon class TreatPatinet() method will get executed and displays the below output:

Surgeon class: I do surgery.
Doctor class: I diagnose patients and prescribe medicine.

Here depending on which type of the instance the doctor variable referring to, that respective type method gets invoked. So here the variable is same and method name called is same, but different implementation of the method will get executed. Its exhibiting different behavior depending on which instance of the class it refers to. So it's a polymorphism happening at runtime.

But why its called run-time polymorphism?

Which implementation of the TreatPatient() method should get executed is dynamic and decided at runtime depending on the instance the base class type variable is holding the reference to. And hence the name runtime polymorphism.

Using Function overriding we can implement run time polymorphism type.

Download Polymorphism demo project(project built on 3.5 .NET using VS 2008)

6 comments:

  1. Simple and effective examples.

    ReplyDelete
  2. Sooper But i could not understand the example.

    ReplyDelete
    Replies
    1. Thanks for the good word.

      What I suggest is download polymorphism tutorial project as mentioned in this article. You can debug the project and see yourself how its working.

      Delete