Because in case of overloading method signature is different, so compiler can decide at compile time which method to call,
where as in case of overriding method signature is same, so method call cannot be resolved at compile time and it depends
upon type of object, and object are created at run time.Hence it is resolved during execution time.
Kindly have a look on below code snippet. While executing it gives the result as “In the SubEnumTest”.
As you can see that “a” is a variable of EnumTest type but at the time of execution it contains an instance of SubEnumTest. In such a scenario to define which function to call depends on the type of object a variable holds which can be identified at run time only.
That is why override is identified at run time. Let me know if your query was something different than I was explaining to.
Mauricio Lopes wrote:I know how and when to use overloading and overwritting, but the point is:
Why overloading methods call is solved on compilation time, and overriden is solved during execution ?
It is not one or the other -- as it is possible to call an overloaded method that can be overridden. The compiler and JVM has to deal with both... And it does so, at both compile and runtime.
Basically, at compile time, the compiler will determine the correct method to call (and any potential overloading) using the references that is being used to call the method. It also determines at compile time whether the method can be overriden (static and private methods can't be overriden).
If the method can be overriden, then at compile time, it will generate the call to the method using the jump table (A table that is used to determine the latest overriden method of any instance).
At runtime, the method is simply called -- using the technique that the compiler configured it for. If the method is called using the jump table then the latest overridden method is called.
Saying that overloading is different from overwritting does not answers the question!
True. But what I was trying to say was that these are two different mechanisms, and there may not be a "why" because of it. Trying to find a reason for two maybe unrelated mechanisms...