Learner Learner - 3 months ago 10
C# Question

How Overloading is Compile Time and Overriding is Runtime?

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....

Answer

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 call, it determines which version of GetMessage() to use at runtime, based on the type of IMessage that is passed in.

Comments