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 ]
posted 13 years ago
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.