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

overriding of static methods

 
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am bit ( a lot actually !) confused on the concept of overriding static methods. for eg in the code

why is the output
Super class static method being called
Sub class static method being called
Super class static method being called
what is meant by the method in the subclass "shadows" the one in the superclass ?
thanx
[ Jess added UBB [CODE] tags to preserve whitespace, check them out! ]
[ August 15, 2002: Message edited by: Jessica Sant ]
 
Ranch Hand
Posts: 1056
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You cannot truly "override" a static method, you can only "hide" or "shadow" the one declared in the superclass.
The decision of which static method to call is made at compile time, not runtime. It depends solely on the declared type of the variable, not on the actual type of the Object that the variable points to.
So, assuming that foo() is declared as a static method...
Thing t;
t = new RedThing();
t.foo();
This is equivalent to calling Thing.foo() . Even if RedThing has its own static foo() method, it won't be called.
Thing t does not even have to be a real Thing. This also works:
Thing t = null;
t.foo();
It calls Thing.foo() and does NOT cause a NullPointerException.
 
My honeysuckle is blooming this year! Now to fertilize this tiny ad:
Garden Master Course kickstarter
https://coderanch.com/t/754577/Garden-Master-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic