• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Static methods

 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm having trouble understanding something about the following code. When run as is, the result is:



I understand that static methods are not overridden, just hidden by the subclass, so which method being invoked is determined at compile time. In this case, the Parent.get1() method is invoked by the inherited get() method of subclass Test, but it knows to use the overridden Test.get2() method.

However, if you change the last line to



the result is



Huh? I'm confused as to how the get() method is using the Parent.get1() method, but when calling Test.get1() it uses the hidden static get1() method in the Test class.

 
Saloon Keeper
Posts: 15510
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When you call get(), control is handed to the parent class. The only get1() method the parent class knows about is its own static method. So that's what it uses. When you change the call in the main method to new Test.get1(), control is in the child class, and it has two different get1() methods it can choose between, the parent's static method, and its own static method. It chooses its own version.
 
Ron Lipke
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Okay that right there makes sense. BUT, when you using that logic, when you hand the call over to the Parent.get() method and it only knows about it's own get1() method, how does it know about the get2() method of the Test class?

 
author
Posts: 23951
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

Ron Lipke wrote:
Huh? I'm confused as to how the get() method is using the Parent.get1() method, but when calling Test.get1() it uses the hidden static get1() method in the Test class.



It's actually the other way around. It is the get1() method of the Parent class that got hidden by the Test class. It is just that your first example got to the hidden method....

Henry
 
Henry Wong
author
Posts: 23951
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

Ron Lipke wrote:Okay that right there makes sense. BUT, when you using that logic, when you hand the call over to the Parent.get() method and it only knows about it's own get1() method, how does it know about the get2() method of the Test class?



At compile time, it doesn't. Polymorphism in Java (and also in C++, and probably tons of other languages) is done with jump tables. For the get2() method, the compiler will generate code to use a jump table, to perform an indirect jump for the method. During instantiation, the table is filled with the correct (ie. the latest version of the) method as the target.

Henry
 
Stephan van Hulst
Saloon Keeper
Posts: 15510
363
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Because get2() is not static. When a subclass overrides a non-static method in its superclass, you could imagine it replaces the body of that method with new code. The superclass still doesn't know about the existence of the subclass' method, it calls its own get2(). However, the body of get2() is being overridden by the subclass. It doesn't exactly work this way, but that should help you understand.
 
Ron Lipke
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Stephan, that makes more sense. Nothing I have learned up to this point says it like that or leads me to figure that out, but now at least I know why it acts the way it does.

Thanks!
reply
    Bookmark Topic Watch Topic
  • New Topic