Wednesday 24 December 2014

What is the difference between compile time and run-time polymorphism

What is the difference between compile time and run-time polymorphism
In many technical interviews for .NET developers role, the important question being asked is, what are two different kinds of polymorphism? OR what is the difference between compile time and runtime polymorphism?
Let's first understand what is all about polymorphism itself.
The WordWeb dictionary says "the existence of two or more forms". Applying this concept to OOPS, polymorphism is all about exhibiting different behaviour depending on what it contains, either at the runtime or at the compile time. Sounds cryptic!, lets start with an example.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
namespace 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);
           // Wait till user enters any character on the console
           Console.Read();
        }
    }
    public static class Calculator
    {
        public static long Add(int operand1, int operand2)
        {
            Console.WriteLine("Two parameter Add() method called");
            long sum = operand1 + operand2;
            return sum;
        }
        public static long Add(int operand1, int operand2, int operand3)
        {
     Console.WriteLine("Three parameter Add() method called");
            long sum = operand1 + operand2 + operand3;
            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 method with the same name Add(). One takes 2 parameters and other takes 3 parameters. Depending on how many parameters we are passing to the 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.
But important interview question is, why its called compile time polymorphism.
Answer is that, which method to call is decided at the compile time only not at the runtime and hence the name compile time polymorphism.

Function overloading is of compile time polymorphism type.
Now let's have a look at the runtime polymorphism.
The best is to go through an example. Let's create a one.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
namespace RuntimePolymorphism
{
    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.
As seen in the above program, Doctor base class and derived class Surgeon. In the Doctor base class we have marked the method TreatPatient() as virtual. That means the derived classes can override the behavior of the base class. Derived class Surgeon is specialized class of Doctor, who can do surgery. And hence TreatPatinet() method is overrided in the Surgeon class.
For the doctor variable when the Doctor class instance [new Doctor()] is assigned and on invoking the TreatPatient() method, the Doctor class TreatPatinent() method gets execute and displayed the below: 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 gets execute and displayed the below:
Surgeon class: I do surgery.
Doctor class: I diagnose patients and prescribe medicine.
Here depending on what which class instance doctor variable reference, that class method gets executed. So here the variable is same and method called is same, but different implementation of the method gets execute. Its exhibiting different behaviour depending on which instance of the class it refers to. So its a polymorphism.
But why its called run time polymorphism?
Answer is simple. Which Implementation of the TreatPatient() method should get executed, is not decided at the compile time and its decided at runtime depending which instance of the class is assigned. And hence the name runtime polymorphism.
Function overriding is of runtime polymorphism type.

No comments:

Post a Comment