Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How Overloading is Compile Time and Overriding is Runtime?

Tags:

c#

.net

oop

Folks

I came across many threads for understanding polymorphism (Both compile time and run time). I was surprised to see some links where the programmers are claiming Overloading is Runtime and Overriding is compile time.

What I want to know from here is:

  1. Runtime Polymorphism with a REAL TIME example and small code and what scenario we should use.
  2. Compile time Polymorphism with REAL TIME example and small code and when to use.

Because I read many theoretical definitions, but I am not satisfied in understanding that.

Also, I gave a thought, that where I also felt, overloading should be runtime as because, say I have a method that calculates Area, at runtime only it decides which overloaded method to call based on parameters I pass (Say if I pass only one parameter, it should fire Square, and if parameters are 2, it should fire Rectangle)....So isn't it I can claim its runtime ? How its complie time ? (Most say theoretically, overloading is compile time but they dont even give a correct REAL time example...very few claim its runtime)....

Also, I feel overriding is compile time because, while you write code and complie, you ensure you used virtual keyword and also overriding that method in derived class which otherwise would give you compile time error. So I feel its compile time, the same way where I saw in a thread.....But most threads claims its runtime :D

I am confused :( This question is additional to my question 1 and 2. Please help with a real time example.. as I am already aware of theoretical definitions .... :(

Thank you....

like image 464
Jasmine Avatar asked Jun 06 '12 14:06

Jasmine


People also ask

Is overloading compile time?

Method overloading is the example of compile time polymorphism and method overriding is the example of run-time polymorphism.

Why overriding is called as runtime?

why overriding is called run time polymorphism? subclass methods will be invoked at runtime. subclass object and subclass method overrides the Parent class method during runtime. its called because it depend on run time not compile time that which method will be called.

Why overloading is compile time polymorphism?

In overloading, the method / function has a same name but different signatures. It is also known as Compile Time Polymorphism because the decision of which method is to be called is made at compile time. Overloading is the concept in which method names are the same with a different set of parameters.

Can we overload methods on runtime?

Java supports overloading methods and can distinguish between methods with different signatures. Consequently, with some qualifications, methods within a class can have the same name if they have different parameter lists. In method overloading, the method to be invoked at runtime is determined at compile time.


2 Answers

In the case of Overloading, you are using static (compile-time) polymorphism because the compiler is aware of exactly which method you are calling. For example:

public static class test {     static void Main(string[] args)     {         Foo();         Foo("test");     }      public static void Foo()     {         Console.WriteLine("No message supplied");     }      public static void Foo(string message)     {         Console.WriteLine(message);     } } 

In this case, the compiler knows exactly which Foo() method we are calling, based on the number/type of parameters.

Overriding is an example of dynamic (runtime) polymorphism. This is due to the fact that the compiler doesn't necessarily know what type of object is being passed in at compile-time. Suppose you have the following classes in a library:

public static class MessagePrinter {     public static void PrintMessage(IMessage message)     {         Console.WriteLine(message.GetMessage());     } }  public interface IMessage {     public string GetMessage(); }  public class XMLMessage : IMessage {     public string GetMessage()     {         return "This is an XML Message";     } }  public class SOAPMessage : IMessage {     public string GetMessage()     {         return "This is a SOAP Message";     } } 

At compile time, you don't know if the caller of that function is passing in an XMLMessage, a SOAPMessage, or possibly another type of IMessage defined elsewhere. When the PrintMessage() function is called, it determines which version of GetMessage() to use at runtime, based on the type of IMessage that is passed in.

like image 89
Jon Senchyna Avatar answered Sep 17 '22 09:09

Jon Senchyna


Read : Polymorphism (C# Programming Guide)

Similar answer : Compile Time and run time Polymorphism

Well, there are two types of Polymorphism as stated below:

  • Static Polymorphism (Early binding)
  • Dynamic Polymorphism (Late binding)

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 example:

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 = tst.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.     } } 

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.

like image 34
Pranay Rana Avatar answered Sep 21 '22 09:09

Pranay Rana