CKTIS / Java
@Override — checking method overriding
The @Override annotation lets the compiler check whether a method overrides a supertype method or implements an interface method. It helps catch typos and accidental overloads; it does not itself change the method's runtime behavior.
Version: Java SE 21; repository runtime, production runtime not attested
Syntax
@Override
public ReturnType methodName(ParameterType parameter) { ... }Place @Override before a method declaration to have the compiler check the intent to override a supertype method. This also covers implementing interface methods, such as Greeting.message() in the example. The annotation does not cause overriding: a valid method overrides its supertype method even without it.
If the declaration does not meet the conditions for using @Override, the compiler reports an error. A misspelled name or changed parameters may create a different method instead of the intended override. Overloading means declaring methods with the same name but different parameter lists; overloading alone does not justify @Override.
A declaration whose signature is override-equivalent to the signature of a public method declared in Object, such as toString(), is also permitted, including in an interface. The annotation targets methods, not classes or constructors. Its retention is SOURCE: it is not stored in the .class file or available through reflection at runtime.
In the example, Hello.message() implements Greeting.message(). Calling it through a variable of type Greeting uses the Hello implementation and should print Hello!. Changing the name only in the implementation to mesage causes a compilation error rather than leaving the mistake unnoticed.
Examples
public class Main {
interface Greeting {
String message();
}
static class Hello implements Greeting {
@Override
public String message() {
return "Hello!";
}
}
public static void main(String[] args) {
Greeting greeting = new Hello();
System.out.println(greeting.message());
}
}Result
Hello!
The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.