Java : @Override (annotation) with Examples
Override (Java SE 20 & JDK 20) API Examples.
You will find code examples on most Override methods.
Summary
public class Base {
public void run() {
System.out.println("Base : run!");
}
}
public class Foo extends Base {
@Override
public void run() {
System.out.println("Foo : run!");
}
}
public class Main {
public static void main(String[] args) {
final var foo = new Foo();
foo.run();
}
}
// --- PowerShell ---
//PS R:\java-work> ls -Name
//Base.java
//Foo.java
//Main.java
//
//PS R:\java-work> javac *.java
//
//PS R:\java-work> java Main
//Foo : run!
An example of a compile error :
public class Foo extends Base {
@Override
public void exe() {
^^^ <---- The method name has been changed!
System.out.println("Foo : exe!");
}
}
// --- PowerShell ---
//PS R:\java-work> javac *.java
//Foo.java:4: error: method does not override or implement a method from a supertype
// @Override
// ^
//1 error
Please see also Java Language Specification :