Java Reference
In-Depth Information
If a method declaration is annotated with the annotation @Override , but the method does not
override or implement a method declared in a supertype, or is not override-equivalent to a
public method of Object , a compile-time error occurs.
This behavior differs from Java SE 5.0, where @Override only caused a compile-time
error if applied to a method that implemented a method from a superinterface that was
not also present in a superclass.
The clause about overriding a public method is motivated by use of @Override in an in-
terface. Consider the following type declarations:
class Foo { @Override public int hashCode() {..} }
interface Bar { @Override int hashCode(); }
The use of @Override in the class declaration is legal by the first clause, because
Foo.hashCode overrides Object.hashCode 8.4.8 ).
For the interface declaration, consider that while an interface does not have Object as
a supertype, an interface does have public abstract members that correspond to the pub-
lic members of Object 9.2 ) . If an interface chooses to declare them explicitly (i.e. to
declare members that are override-equivalent to public methods of Object ), then the in-
terface is deemed to override them (§ 8.4.8 ) , and use of @Override is allowed.
However, consider an interface that attempts to use @Override on a clone method: ( final-
ize could also be used in this example)
interface Quux { @Override Object clone(); }
Because Object.clone is not public , there is no member called clone implicitly declared
in Quux . Therefore, the explicit declaration of clone in Quux is not deemed to “imple-
ment” any other method, and it is erroneous to use @Override . (The fact that Quux.clone
is public is not relevant.)
In contrast, a class declaration that declares clone is simply overriding Object.clone , so
is able to use @Override :
class Beep { @Override protected Object clone() {..} }
9.6.3.5. @SuppressWarnings
Java compilers are increasingly capable of issuing helpful “lint-like” warnings. To encour-
age the use of such warnings, there should be some way to disable a warning in a part of
the program when the programmer knows that the warning is inappropriate.
The annotation type SuppressWarnings supports programmer control over warnings otherwise
issued by a Java compiler. It contains a single element that is an array of String .
Search WWH ::




Custom Search