Win a copy of Escape Velocity: Better Metrics for Agile Teams this week in the Agile and Other Processes forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Frank Carver
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • fred rosenberger

Overloading - overwriting difference ?

 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
On my way studying for SCJP i've found something slightly strange about method's overloading. Why it works diferent from overwritting? Take a look below:




Somewhere else you may instance:




the result is // print A. The method choice is solved on compilation time.

But overwritting works different like below:



Somewhere else:



The method choice is made on execution time. // print b


Why such a difference between overloading and overwritting ???
 
author
Posts: 23928
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Why such a difference between overloading and overwritting ???



Because overloading and overriding are two different mechanisms?

Henry
 
Ranch Hand
Posts: 99
Android Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Because first is example overloading in which method is determined by reference type and not object type,
which is done at compile time.

And in second case you are doing overriding in which method is selected based on object type created at run time and
not on reference type.
 
Ranch Hand
Posts: 449
Scala IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Mauricio Lopes wrote:
Somewhere else you may instance:
the result is // print A. The method choice is solved on compilation time.


Prints "A" b/c the method "doXXX" is overloaded not overridden. When you call a method "a.doXXX( new B() ) " it will use the reference type and sees only one method "doXXX" in class A.

The name and parameters must be same for overriding a method. Since the parameter type is different in your case it is overloading, not overriding.

Also see this

 
Mauricio Lopes
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ?


Saying that overloading is different from overwritting does not answers the question!
 
Anuj Prashar
Ranch Hand
Posts: 99
Android Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 257
Hibernate Oracle Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey Mauricio,

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.

 
Henry Wong
author
Posts: 23928
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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

Henry
 
Ranch Hand
Posts: 1880
Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
overridding is runtime polymorphism and one of the good features in Java. It allows you to invoke the method on runtime.
 
Mauricio Lopes
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Henry you got my question.

Thanks for the effort to solve my doubt. I think it got my mind relaxed.

 
That feels good. Thanks. Here's a tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic