Sub topic 10
Check Override Rules with @Override
In the last chapter, you wrote a method in a child class with the same name as a method in its parent class. This is called overriding. Overriding means a subclass gives its own version of a method it inherited. Java has a helpful tool that checks this work. It is called @Override.
What @Override Does
An annotation is a label you place before a method. The label tells the compiler what you intend. The compiler is the part of Java that checks your code before it runs. The @Override label does not change how your method works. It only asks the compiler to confirm that your method really does override a parent method.
A Working Example
Here is a parent class with a speak method, and a child class that overrides it.
class Animal {
String speak() {
return "...";
}
}
class Dog extends Animal {
@Override
String speak() {
return "Woof";
}
}
public class Main {
public static void main(String[] args) {
Animal pet = new Dog();
System.out.println(pet.speak());
}
}The output is Woof. The variable pet has the type Animal, but the object is a Dog. Java runs the Dog version of speak. You will learn more about this behavior in the next chapter.
The Rules for Overriding
The compiler checks several rules. Your method must follow them to override a parent method.
| Rule | What it means |
|---|---|
| Same name | The method name must match exactly, including capital letters. |
| Same parameters | The list of inputs must have the same types in the same order. |
| Same or narrower return type | The return type must be the same, or a more specific type. |
| Access not reduced | A `public` parent method must stay `public` in the child. |
| No final methods | A method marked `final` in the parent cannot be overridden. |
| No private methods | A `private` parent method is not inherited, so it cannot be overridden. |
If any rule breaks, the compiler gives an error when @Override is present.
A Common Mistake
Suppose you type a capital letter by mistake:
@Override
void Speak() {
System.out.println("Woof");
}Java treats Speak and speak as different names. This method does not override anything. Without @Override, the code still compiles. The parent method runs instead, and you get no warning. This kind of bug is hard to find.
With @Override, the compiler stops and shows this error: method does not override or implement a method from a supertype. The label catches the mistake right away.
> Tip: Write @Override on every method you intend to override. It costs one line and catches typing errors early.
> Note: Java names are case-sensitive. run, Run, and RUN are three different names.
Summary
@Overrideis an annotation that asks the compiler to check your override.- The method name, parameters, return type, and access level must follow the rules.
- The label catches typing mistakes that would otherwise fail silently.
Try It
Exercise
A parent class has a method void run(). A child class has @Override void Run(). What happens when you compile the code?
Show answer
The compiler reports an error, because Run does not match run. Java is case-sensitive. To fix it, rename the method to run() so it matches the parent.