Java Reference
In-Depth Information
The Override Annotation Type
The override annotation type is a marker annotation type. It can only be used on methods. It indicates that a method
annotated with this annotation overrides a method declared in its supertype. In Java 5, it could be used only in class
methods. From Java 6, it can be used for methods of any types. This is very helpful for developers to avoid types that
lead to logical errors in the program. If you mean to override a method in a supertype, it is recommended to annotate
the overridden method with a @Override annotation. The compiler will make sure that the annotated method really
overrides a method in the supertype. If the annotated method does not override a method in the supertype, the
compiler will generate an error.
Consider two classes, A and B . Class B inherits from class A . The m1() method in the class B overrides the m1()
method in its superclass A . The annotation @Override on the m1() method in class B just makes a statement about this
intention. The compiler verifies this statement and finds it to be true in this case.
public class A {
public void m1() {
}
}
public class B extends A {
@Override
public void m1() {
}
}
Let's consider class C.
// Won't compile because m2() does not override any method
public class C extends A {
@Override
public void m2() {
}
}
The method m2() in class C has a @Override annotation. However, there is no m2() method in its superclass A .
The method m2() is a new method declaration in class C . The compiler finds out that method m2() in class C does
not override any superclass method, even though its developer has indicated so. The compiler generates an error in
this case.
The SuppressWarnings Annotation Type
The SuppressWarnings is used to suppress named compiler warnings. It declares one element named value whose
data type is an array of String . Let's consider the code for the SuppressWarningsTest class, which uses the raw type
for the ArrayList<T> in the test() method. The compiler generates an unchecked named warning when you use a
raw type.
// SuppressWarningsTest.java
package com.jdojo.annotation;
import java.util.ArrayList;
 
Search WWH ::




Custom Search