Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Compile time polymorphism vs. run time polymorphism

Tags:

c#

oop

Why is overloading called compile time polymorphism and Overriding run time polymorphism in C#?

like image 211
Saurabh Avatar asked Jan 28 '10 07:01

Saurabh


People also ask

What is polymorphism runtime and compile time polymorphism with example?

Compile-time polymorphism is also known as static polymorphism or early binding. Compile-time polymorphism is a polymorphism that is resolved during the compilation process. Overloading of methods is called through the reference variable of a class.

What is runtime polymorphism with example?

Java For TestersMethod overriding is an example of runtime polymorphism. In method overriding, a subclass overrides a method with the same signature as that of in its superclass. During compile time, the check is made on the reference type.

What is difference between compile and runtime?

Compile time is the period when the programming code (such as C#, Java, C, Python) is converted to the machine code (i.e. binary code). Runtime is the period of time when a program is running and generally occurs after compile time.


2 Answers

Well, overloading decisions (which method signatures are used, based on the arguments1) are made by the compiler, whereas overriding decisions (which method implementations are used, based on the type of the target of the method) are made by the CLR at execution time.

I wouldn't usually call overloading "polymorphism" though. In my experience the word usually refers to overriding. I suppose overloading does allow you to treat an object of one type as another, although overloading itself doesn't need to be involved there - it's just normal type conversions.

Here's an example showing that overload choice is performed at compile time:

using System;  class Test {     static void Foo(object a)     {         Console.WriteLine("Object overload called");     }      static void Foo(string a)     {         Console.WriteLine("String overload called");     }      static void Main()     {         object x = "hello";         Foo(x);     } } 

Here the Foo(object) overload is called because x is of type object at compile time - it's only at execution time that it's known to refer to a string.

Compare that with this example:

using System;  class Base {     public virtual void Foo()     {         Console.WriteLine("Base.Foo called");     } }  class Derived : Base {     public override void Foo()     {         Console.WriteLine("Derived.Foo called");     } }  class Test {     static void Main()     {         Base x = new Derived();         x.Foo();     } } 

Here the compile-time type of x is Base, but it's still the derived class's overriding method which is called, because the execution-time type of the object that x refers to is Derived.


1 It's slightly more complicated than that in fact, due to method hiding etc - but in simple cases you can think of it as just picking the signature.

like image 122
Jon Skeet Avatar answered Oct 08 '22 13:10

Jon Skeet


Overridden functions are functions that have the same signature, but are implemented in different derived classes. At compile time, usually the base class type is used to reference an object, though at run time this object could be of a derived type, so when an overridden method is called, the implementation that is called is dependent on what kind of object is doing the calling (base vs. a derived type) which is unknown at compile time.

Overloading (not really polymorphism) is simply multiple functions which have the same name but different signatures (think multiple constructors for an object taking different numbers of arguments). Which method is called is known at compile time, because the arguments are specified at this time.

like image 27
Nate Heinrich Avatar answered Oct 08 '22 15:10

Nate Heinrich