Skip to lesson

The @Override annotation

@Override is a note above a method that tells the compiler: "this method must override a parent method". If it does not, javac stops with an error, so a typo can never slip through silently.

The silent bug

Overriding needs the exact name and parameters. One wrong letter, sendd instead of send, makes a brand-new method. Java gives no error: the program runs, but the parent version prints.

Example 1 · A typo with no warning

TypoBugDemo.javaJava
class Notification {    void send(String msg) {        System.out.println("Notice: " + msg);    }}
class SmsNotification extends Notification {    void sendd(String msg) {        System.out.println("SMS: " + msg);    }}
public class TypoBugDemo {    public static void main(String[] args) {        new SmsNotification().send("Fee due");    }}
Output
Notice: Fee due

What this code does

  • sendd has an extra d, so it does not override send. It is a new method that nobody calls.
  • send runs the inherited parent version. The program compiles and runs, but prints the wrong message.

The cause is hard to see, because nothing looks wrong. You only notice when the output is wrong, sometimes after the app is already with users.

What @Override does

@Override is an annotation that marks a method as an override. The compiler then checks that the class really inherits a method with the same signature; if not, compilation fails. It changes nothing when the program runs.

An annotation is a note for the compiler that starts with @ and sits above a method. Add @Override above sendd and the hidden typo becomes an error.

Example 2 · @Override turns the typo into an error

TypoDemo.javaJava
class Notification {    void send(String msg) {        System.out.println("Notice: " + msg);    }}
class SmsNotification extends Notification {    @Override    void sendd(String msg) {        System.out.println("SMS: " + msg);    }}
public class TypoDemo {    public static void main(String[] args) {        new SmsNotification().send("Fee due");    }}
Sample compiler output
TypoDemo.java:8: error: method does not override or implement a method from a supertype
    @Override
    ^
1 error

What this code does

  • The only change is @Override on line 8. The compiler looks for sendd(String) in Notification and does not find it.
  • The bug is caught before the program ever runs. Rename sendd to send and it compiles and prints SMS: Fee due.

The error names a supertype: a parent type, such as the parent class. @Override only adds a check at compile time; a correct program prints exactly the same with or without it.

What @Override catches

Besides typos, @Override catches a wrong parameter type and a parent method that is private. Each would otherwise compile as a quiet new method.

Example 3 · A wrong parameter type is caught too

ParamDemo.javaJava
class Fee {    double total(int months) {        return months * 1000;    }}
class HostelFee extends Fee {    @Override    double total(double months) {        return months * 1500;    }}
public class ParamDemo {    public static void main(String[] args) {        System.out.println(new HostelFee().total(2));    }}
Sample compiler output
ParamDemo.java:8: error: method does not override or implement a method from a supertype
    @Override
    ^
1 error

What this code does

  • The name is right, but the parent takes an int and the child takes a double. Without @Override this would quietly be an overload.
  • Change the parameter to int months and it compiles and prints 3000.0.

Example 4 · @Override cannot override a private method

PrivateAuditDemo.javaJava
class Account {    private void audit() {        System.out.println("Account audit");    }}
class Savings extends Account {    @Override    void audit() {        System.out.println("Savings audit");    }}
public class PrivateAuditDemo {    public static void main(String[] args) {        new Savings().audit();    }}
Sample compiler output
PrivateAuditDemo.java:8: error: method does not override or implement a method from a supertype
    @Override
    ^
1 error

What this code does

  • audit() in Account is private, so Savings does not inherit it and there is nothing to override.
  • Without @Override this compiles as a separate method. The annotation tells you that your picture of the class is wrong.

@Override is not allowed on a static method. The error is static methods cannot be annotated with @Override, because static methods are hidden, never overridden.

Find the bug

Which line does javac reject?

Select the line that is wrong.

Payment has void pay(int amount). Upi should replace it.

Round 1 of 4

Make it a habit

@Override is optional: a correct override works without it. Still, write it on every overriding method. Readers see at once which methods replace a parent version, and mistakes fail loudly.

Example 5 · The correct way

OverrideDemo.javaJava
class Notification {    void send(String msg) {        System.out.println("Notice: " + msg);    }}
class SmsNotification extends Notification {    @Override    void send(String msg) {        System.out.println("SMS: " + msg);    }}
class EmailNotification extends Notification {    @Override    void send(String msg) {        System.out.println("Email: " + msg);    }}
public class OverrideDemo {    public static void main(String[] args) {        new SmsNotification().send("Fee due");        new EmailNotification().send("Results are out");    }}
Output
SMS: Fee due
Email: Results are out

What this code does

  • Each override has @Override and matches send(String) exactly, so it compiles.
  • The output is the same as without the annotation. It only adds safety and makes the code easier to read.

IDEs such as IntelliJ IDEA, Eclipse and VS Code add @Override for you when they generate an override. From here on, every override in this course uses it, starting with super.method() (opens in a new tab).

Next step

Check your understanding

11 quick questions