Originally posted by derek_ho:
Is this true:
"static methods can be overridden by static methods only".( from java_cert exam)
The answer is false. can someone tell me why?
can static method be overriden? If yes, what are ways of overriding a static method?
No, it is not true. Static methods cannot be overridden, rather, they can be hidden.
This program shows the difference between method hiding and overridding.
As you can see, line 1 works as expected. You are making a method call on an SuperClass object reference variable, but because the actual instance is of type SubClass, the overridden method gets called.
In line 2, when we attempt to do the same thing with a static method, we see that the same rules do not apply. The static method that gets invoked is the one that is a member of the reference variable's class, NOT the one that is a member of the actual object instance!
This shows that there is a very real, tangible, important distinction between static and instance methods, and between hiding and overridding. It also shows why it is Bad Form to refer to a static member by an reference variable; you should always use the class name to access a static member. In the above line 2, if you had written
it would be clear which method would be invoked.