CodeQL documentation

Missing Override annotation

ID: java/missing-override-annotation
Kind: problem
Security severity: 
Severity: recommendation
Precision: high
Tags:
   - quality
   - maintainability
   - readability
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

  • © GitHub, Inc.
  • Terms
  • Privacy
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy