In JLS 220.127.116.11 it is given If a class declares a static method ,then the declaration of that method is said to hide any and all methods with the same signature in the superclasses and the superinterfaces of the class that would otherwise be accessible to the code in the class. The interface methods are implicitly abstract.static modifier cannot be applied to abstract methods.If this is the case, how come a static method in a class can hide a method with same signature in the superinterface? Thanks, vinu.
Excellent point! I can't think of a situation where it's possible for a static method to hide a method with the same signature in the superinterface. When I try, the compiler error is: MyClass.java:14: m1() in MyClass cannot implement m1() in SuperInterface; overriding method is static
If anyone can figure how the JLS is right about this, I'll be much obliged. If not, I'll chalk it up to a careless oversight in the JLS.
I believe you're correct: there is no way for a static method to legally hide a method of the same signature in a superinterface. However this isn't a problem with the JLS. Consider the fuller context from JLS2 18.104.22.168 (or JLS3 22.214.171.124 which says basically the same thing):
If a class declares a static method, then the declaration of that method is said to hide any and all methods with the same signature in the superclasses and superinterfaces of the class that would otherwise be accessible to code in the class. A compile-time error occurs if a static method hides an instance method.
The first sentence defines the term hide - that doesn't necessarily imply that hiding is always legal. The second sentence specifies a particular circumstance in which hiding is not legal. All interface methods are instance methods, and so when it says a static method can't hid an instance method, that eliminates hiding of any interface methods. If they hadn't included interfaces in the first sentence, then the second sentence would not have applied, and we'd be left wondering whether or not it's legal to declare a static method with the same signature as a superinterface method. [ May 01, 2005: Message edited by: Jim Yingst ]