locked
Compile Time and run time Polymorphism RRS feed

  • Question

  • I like to know why method overloading is compile time Polymorphism amd method overriding is run time. what is logic behind that?
    Monday, August 16, 2010 4:50 AM

Answers

  • Hello Amit_Ks,

        Lets have an example for understanding of the same:

    Well, there are two types of Polymorphism as stated below:
    1. Static Polymorphism (Early binding) 
    2. Dynamic Polymorphism (Late binding)

    1. Static Polymorphism(Early Binding):

        Static Polymorphism is also know as Early Binding and Compile time Polymorphism. Method Overloading and Operator Overloading are examples of the same.

        It is known as Early Binding because the compiler is aware of the functions with same name and also which overloaded function is tobe called is known at compile time.

    For exa:

    public class Test()

    {

        public Test()

       {

       }

      public int add(int no1, int no2)

         {

         }

    public int add(int no1, int no2, int no3)

         {

         }

    }

    class Program
        {
            static void Main(string[] args)
            {
                Test tst = new Test ();
                 int sum = an.add(10,20);           

           // here in above statement compiler is aware at compile time that need to call function add(int no1, int no2), hence it is called early binding and it is fixed so called static binding.
            }
        }

    2. Dynamic Polymorphism(Late Binding):

     public class Animal
        {
            public virtual void MakeSound()
            {
                Console.WriteLine("Animal sound");
            }
        }
        public class Dog:Animal
        {
            public override void MakeSound()
            {
                Console.WriteLine("Dog sound");
            }
           
        }

        class Program
        {
            static void Main(string[] args)
            {
                Animal an = new Dog();
                an.MakeSound();          
                Console.ReadLine();
            }
        }

      As in the above code , as any other call to a virtual method, will be compiled to a callvirt IL instruction. This means that the actual method that gets called is determined at run-time (unless the JIT can optimize some special case), but the compiler checked that the method exists, it chose the most appropriate overload (if any) and it has the guarantee that the function pointer will exist at a well-defined location in the vtable of the type (even though that is an implementation detail). The process of resolving the virtual call is extremely fast (you only need to dereference a few pointers), so it doesn't make much of a difference.

    This is not the same as "true" late binding, that you can obtain in C# by calling methods through reflection or via the dynamic type (in C#4): in that case the method is not guaranteed to exist and overloading is also resolved at run-time. The overall process is way more expensive.
    So, compiler is not aware while compilation is going on which function will be overriden as they will come in to effect runtime when an object of base class or derived class is instantianted and then which method is to be called is decided so it is known as Late Binding.

    Thanks,

    Paras Sanghani

    http://parassanghani.blogspot.com/

    Mark As Answer if it helped you.

    • Proposed as answer by Manthan Makwana Monday, August 16, 2010 6:15 AM
    • Marked as answer by Figo Fei Wednesday, August 18, 2010 5:43 AM
    Monday, August 16, 2010 6:02 AM
  • Hi,

    Thanks for posting.

    It's because function overloads are resolved at the compile time. This is called static or earlier binding. And virtual methods overrides are resolved at the runtime. This is called dynamic or late binding.

    For an example, you can compare "var x" in JavaScript and "int x" in C#. "int x" in C# is earlier binding i.e. we force the x type earlier but "var x" in JavaScript is late binding because the type of x will be resolved at runtime.

    Hope this helps some.

    Sincerely,

    Yasser


    LEARN "LINQ TO DATASET" DURING A QUICK SIMPLE HOW TO:
    How To: Joining multiple DataTable using “LINQ to DataSet”
    • Proposed as answer by Brad Lane Monday, August 16, 2010 5:56 AM
    • Marked as answer by Figo Fei Wednesday, August 18, 2010 5:43 AM
    Monday, August 16, 2010 5:54 AM

All replies

  • Hi,

    Thanks for posting.

    It's because function overloads are resolved at the compile time. This is called static or earlier binding. And virtual methods overrides are resolved at the runtime. This is called dynamic or late binding.

    For an example, you can compare "var x" in JavaScript and "int x" in C#. "int x" in C# is earlier binding i.e. we force the x type earlier but "var x" in JavaScript is late binding because the type of x will be resolved at runtime.

    Hope this helps some.

    Sincerely,

    Yasser


    LEARN "LINQ TO DATASET" DURING A QUICK SIMPLE HOW TO:
    How To: Joining multiple DataTable using “LINQ to DataSet”
    • Proposed as answer by Brad Lane Monday, August 16, 2010 5:56 AM
    • Marked as answer by Figo Fei Wednesday, August 18, 2010 5:43 AM
    Monday, August 16, 2010 5:54 AM
  • Hello Amit_Ks,

        Lets have an example for understanding of the same:

    Well, there are two types of Polymorphism as stated below:
    1. Static Polymorphism (Early binding) 
    2. Dynamic Polymorphism (Late binding)

    1. Static Polymorphism(Early Binding):

        Static Polymorphism is also know as Early Binding and Compile time Polymorphism. Method Overloading and Operator Overloading are examples of the same.

        It is known as Early Binding because the compiler is aware of the functions with same name and also which overloaded function is tobe called is known at compile time.

    For exa:

    public class Test()

    {

        public Test()

       {

       }

      public int add(int no1, int no2)

         {

         }

    public int add(int no1, int no2, int no3)

         {

         }

    }

    class Program
        {
            static void Main(string[] args)
            {
                Test tst = new Test ();
                 int sum = an.add(10,20);           

           // here in above statement compiler is aware at compile time that need to call function add(int no1, int no2), hence it is called early binding and it is fixed so called static binding.
            }
        }

    2. Dynamic Polymorphism(Late Binding):

     public class Animal
        {
            public virtual void MakeSound()
            {
                Console.WriteLine("Animal sound");
            }
        }
        public class Dog:Animal
        {
            public override void MakeSound()
            {
                Console.WriteLine("Dog sound");
            }
           
        }

        class Program
        {
            static void Main(string[] args)
            {
                Animal an = new Dog();
                an.MakeSound();          
                Console.ReadLine();
            }
        }

      As in the above code , as any other call to a virtual method, will be compiled to a callvirt IL instruction. This means that the actual method that gets called is determined at run-time (unless the JIT can optimize some special case), but the compiler checked that the method exists, it chose the most appropriate overload (if any) and it has the guarantee that the function pointer will exist at a well-defined location in the vtable of the type (even though that is an implementation detail). The process of resolving the virtual call is extremely fast (you only need to dereference a few pointers), so it doesn't make much of a difference.

    This is not the same as "true" late binding, that you can obtain in C# by calling methods through reflection or via the dynamic type (in C#4): in that case the method is not guaranteed to exist and overloading is also resolved at run-time. The overall process is way more expensive.
    So, compiler is not aware while compilation is going on which function will be overriden as they will come in to effect runtime when an object of base class or derived class is instantianted and then which method is to be called is decided so it is known as Late Binding.

    Thanks,

    Paras Sanghani

    http://parassanghani.blogspot.com/

    Mark As Answer if it helped you.

    • Proposed as answer by Manthan Makwana Monday, August 16, 2010 6:15 AM
    • Marked as answer by Figo Fei Wednesday, August 18, 2010 5:43 AM
    Monday, August 16, 2010 6:02 AM
  • Thanks!.

    Really Helpfull. but if we use reflection and load a class at runtime which are having overloaded methods in that case we can say that this is run time Polymorphism.

    Monday, August 16, 2010 8:27 AM
  •        

    The process of resolving the virtual call is extremely fast (you only need to dereference a few pointers), so it doesn't make much of a difference.

    Thanks,

    Paras Sanghani

    http://parassanghani.blogspot.com/

    Mark As Answer if it helped you.

    it depends on the size of the object ( inheritance  depth ) . More V-table through hierarchy then probably it will take more time to look up.


    Monday, August 16, 2010 10:25 AM