The @Override annotation
25 min · Beginner
Need help? Ask a tutor on WhatsApp (opens in a new tab)@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
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"); }}Notice: Fee due
What this code does
senddhas an extrad, so it does not overridesend. It is a new method that nobody calls.sendruns 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
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"); }}TypoDemo.java:8: error: method does not override or implement a method from a supertype
@Override
^
1 errorWhat this code does
- The only change is
@Overrideon line 8. The compiler looks forsendd(String)inNotificationand does not find it. - The bug is caught before the program ever runs. Rename
senddtosendand it compiles and printsSMS: 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
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)); }}ParamDemo.java:8: error: method does not override or implement a method from a supertype
@Override
^
1 errorWhat this code does
- The name is right, but the parent takes an
intand the child takes adouble. Without@Overridethis would quietly be an overload. - Change the parameter to
int monthsand it compiles and prints3000.0.
Example 4 · @Override cannot override a private method
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(); }}PrivateAuditDemo.java:8: error: method does not override or implement a method from a supertype
@Override
^
1 errorWhat this code does
audit()inAccountisprivate, soSavingsdoes not inherit it and there is nothing to override.- Without
@Overridethis 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 4Make 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
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"); }}SMS: Fee due Email: Results are out
What this code does
- Each override has
@Overrideand matchessend(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