Missing Override annotation¶
ID: java/missing-override-annotation
Kind: problem
Security severity:
Severity: recommendation
Precision: high
Tags:
- maintainability
Query suites:
- java-security-and-quality.qls
Click to see the query in the CodeQL repository
Java enables you to annotate methods that are intended to override a method in a superclass. Compilers are required to generate an error if such an annotated method does not override a method in a superclass, which provides increased protection from potential defects. An annotated method also improves code readability.
Recommendation¶
Add an @Override
annotation to a method that is intended to override a method in a superclass.
Example¶
In the following example, Triangle.getArea
overrides Rectangle.getArea
, so it is annotated with @Override
.
class Rectangle
{
private int w = 10, h = 10;
public int getArea() {
return w * h;
}
}
class Triangle extends Rectangle
{
@Override // Annotation of an overriding method
public int getArea() {
return super.getArea() / 2;
}
}
References¶
J. Bloch, Effective Java (second edition), Item 36. Addison-Wesley, 2008.
Help - Eclipse Platform: Java Compiler Errors/Warnings Preferences.
Java API Specification: Annotation Type Override.
The Java Tutorials: Predefined Annotation Types.