A static method can be overridden as long as the overriding method is also static. Quote from The Java Tutorial: "Also, a subclass cannot override methods that are declared static in the superclass. In other words, a subclass cannot override a class method. A subclass can hide a static method in the superclass by declaring a static method in the subclass with the same signature as the static method in the superclass." The Java Tutorial This compiles:
While this doesn't:
[ April 13, 2002: Message edited by: Clive Rodrigues ]
Originally posted by Clive Rodrigues: A static method can be overridden as long as the overriding method is also static.
Clive, that's not quite accurate. The concept of hiding vs overriding for static methods may seem subtle at first, but after a while you really start to understand what the differences mean. While it's true that you can say "a static method cannot be overriden by an instance method" and vice versus, that does not imply that therefore a static method can override a static method. Static methods, also known as "class methods", belong to the Class itself. Static method calls are never made polymorphically; that's the biggest difference between those and instance methods. Static methods can only hide static methods in the superclass. Just remember, you can override instance methods, but static methods are NEVER overriden; they can be hidden, however.
Joined: Mar 29, 2002
Thanks Rob, I now understand the subtle difference between overriding and hiding. Overriding enables polymorphism while hiding doesn't apply to polymorphism since the methods are static. [ April 14, 2002: Message edited by: Clive Rodrigues ]