Can anyone explain why overriding is a runtime polymorphism and overloading is a compile time polymorphisms ?!?!! | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 5

Can anyone explain why overriding is a runtime polymorphism and overloading is a compile time polymorphisms ?!?!!

I know that methods stored in stak one by one ..., every method invoked stored in the stak !!! please explain that

18th May 2016, 3:53 PM
Rand Hazem
3 Answers
+ 13
Just think in terms of when a method is picked for execution. If there are overridden methods, which method is to be executed depends on the type of object ; that is decided at RUNTIME by the JVM. So this is "runtime polymorphism". If a method is overloaded, the compiler decides which method is to be executed by verifying the argument list and return type at compile time. So this is "compile time polymorphism". I hope I was clear. :)
24th Jun 2016, 1:14 PM
Beardman13
+ 2
With overloading you have a function with different sets of parameters. The function that is executed is determined using the number and type of the parameters you provide. As these are known at compile time, the compiler already determines the function to use. Because of this, it is called compile time polymorphism. When you are overriding a virtual function of a base class in one or more derived classes and then call this function from a base class poniter, the actual class of the underlying object is not clear at compile time. Thus it is determined only at runtime which function is executed. That is why it is called runtime polymorphism. I have just realized that This question is tagged java and not c++. In that case substitute pointer by reference.
22nd Jun 2016, 9:21 PM
Max Dörner
0
One more point to add maybe. AFAIK, Java doesn't consider return type as a distinctive element when doing name mangling.
31st Jul 2018, 9:55 AM
Zeki Gulser