Sub topic 4
Letting the Compiler Check You with @Override
In the last chapter you overrode a method by writing a method in the subclass with the same name and parameters as one in the superclass. That works, but nothing stops you from making a small typo. Java has a tool that catches those typos before your program runs.
What @Override Is
An annotation is a note you attach to code. It starts with @. Java reads it, but it does not change what your program does when it runs.
@Override is an annotation you put on a method. It tells the compiler: "This method is meant to override a method from the superclass." The compiler is the program (javac) that turns your Java code into something the computer can run. When it sees @Override, it checks that a matching method really exists in the superclass. If no matching method exists, it stops with an error.
Why It Matters: A Silent Bug
Here is a Dog class with a typo. The method is spelled sond instead of sound.
class Animal {
String sound() {
return "...";
}
}
class Dog extends Animal {
String sond() { // typo: should be sound()
return "Woof";
}
}
public class Main {
public static void main(String[] args) {
Animal a = new Dog();
System.out.println(a.sound());
}
}This program compiles and runs. It prints ..., not Woof.
Java sees sond() as a brand-new method. It does not replace sound(), so Dog still uses the sound() it got from Animal. You get no error and no warning, just the wrong output.
The Same Code with @Override
Now add @Override above the method:
class Animal {
String sound() {
return "...";
}
}
class Dog extends Animal {
@Override
String sond() { // typo is still here
return "Woof";
}
}This time the compiler refuses to build it:
error: method does not override or implement a method from a supertype
@Override
^The bug shows up the moment you compile, with the line number. You fix the name to sound, and the program prints Woof.
Mistakes @Override Catches
| Mistake | Example | Without @Override | With @Override |
|---|---|---|---|
| Misspelled name | `sond()` | New method, silent bug | Compile error |
| Wrong capitalization | `Sound()` | New method, silent bug | Compile error |
| Different parameters | `sound(int times)` | New method, silent bug | Compile error |
| Superclass method later renamed | `sound()` renamed to `noise()` | Old override stops working | Compile error |
Java names are case-sensitive, so Sound and sound count as different names. All four mistakes give you a new method instead of an override.
Exercise
A class Cat extends Animal and declares @Override String sound(String mood) { return "Meow"; }. Animal only has String sound(). Will this compile? Why or why not?
Show answer
No. The parameters are different (one String parameter instead of none), so this method does not override Animal's sound(). Because it is marked @Override, the compiler reports "method does not override or implement a method from a supertype".
Exercise
You remove @Override from the Cat method in the previous question. Does it compile, and what does Animal a = new Cat(); a.sound() return?
Show answer
It compiles, because sound(String mood) is now just a new, separate method. Calling a.sound() runs Animal's version and returns "...".