• 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

Overriding static methods!

 
Ranch Hand
Posts: 111
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all.
Just finished yet another mock exam. Came across this question: Can a static method override a static method?. I answered yes, the answer given was no. Confused I wrote the following code.
Here is the code:

I compiles & runs fine. Now I'm not trying to be clever pointing out this error. I just want clarification that the answer given by myself was either right or wrong?
Thanks, Dave.
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Dave,
"Overriding" specifically implies polymorphism, so that, in your example, whether G.aMethod() or F.aMethod() were called would depend on the runtime type of some object. But all you have here is "hiding" -- which method will be called is decided at compile time, not runtime. This is similar to declaring a member variable x in both F and G; the x in the child will simply hide the one in the parent; the parent's methods will continue to use the one in the parent.
 
Ranch Hand
Posts: 1561
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yep, have a look at this:

what's the result?
 
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It will print "hello from A" becoz method has been declared static.
sumukh
 
reply
    Bookmark Topic Watch Topic
  • New Topic