I am afraid you are mistaken; static members are neither inherited (according to the Java Language Specification) nor overridden. The metodo method reappears in "B" because "B extends A" means a B IS-A(n) A. Try this in Java5 or Java6:You ought to get a compiler error.
Static members (and instance fields) can be hidden not overridden (we have an FAQ about that). Please do a search of JavaRanch; there have been several threads on very similar subjects very recently.
[Campbell]: I am afraid you are mistaken; static members are neither inherited (according to the Java Language Specification) nor overridden.
I certainly agree that static methods are not overridden. But according to the JLS definition, they are inherited:
The members of a class type (�8.2) are classes (�8.5, �9.5), interfaces (�8.5, �9.5), fields (�8.3, �9.3, �10.7), and methods (�8.4, �9.4). Members are either declared in the type, or inherited because they are accessible members of a superclass or superinterface which are neither private nor hidden nor overridden (�8.4.8).
Nothing in that definition prevents statics from being considered inherited. Yes, statics behave differently than nonstatics when it comes to overriding vs. overloading. But that doesn't mean they aren't inherited.
[Campbell]: Try this in Java5 or Java6:
As I recall, @Override is not enforced in JDK 5. Unless they added that in a later bugfix release, but I doubt it.
Also, note that the rules for @Override are not exactly the same as the JLS definition of overriding. For example, JLS 9.4.1 makes clear that an interface method may override a method declared in a superinterface. But if you try using @Override in this case, it's not allowed. So we can't assume that @Override matches the JLS definitions.
Joined: Oct 13, 2005
I was obviously mistaken about inheritance. Sorry. @Override was introduced in Java5. And we are not working in an interface, but in a concrete class, where putting @Override before a static method ought to produce a compiler error.
Joined: Mar 05, 2008
[Campbell]: @Override was introduced in Java5.
Introduced, yes. But not enforced. It was ignored by the compiler, whether or not it was on a method that actually overrode anything (by any definition). If you want Victor to observe a compiler error for the example given, he probably needs to use JDK 6, not 5.
[Campbell]: And we are not working in an interface, but in a concrete class, where putting @Override before a static method ought to produce a compiler error.
I'm aware of that. My general point was that @Override does not map directly to "overriding" as defined in the JLS. The folks who defined the @Override annotation were apparently on crack or something. Whether or not @Override compiles in a given program has little bearing on whether it's really overriding (in the JLS sense) or not. To put it another way, I agree with your conclusion (static methods can't be overridden) but not with your reasoning (because the @Override meaning is different from the JLS meaning of override). Yeah, it's nice that @Override seems to support your (correct) conclusion, but that's just coincidence as far as I'm concerned. The @Override annotation is not to be trusted as a definition of what "override" means.