Sub topic 11
Distinguish Overloading and Hiding from Overriding
Java lets a class reuse names it already has in several ways. Overriding, overloading, and hiding all reuse names, and they look alike. They work differently, though, and mixing them up causes bugs. This chapter shows how to tell them apart.
Overriding happens when a subclass (a class that extends another class) writes a method with the same name and the same parameters as a method in its superclass (the class it extends). The new method replaces the old one for objects of the subclass.
Four Ideas That Look Alike
The key question is which version of a method or field runs. The answer depends on the situation.
| Idea | Same name? | Same parameters? | Which version is chosen | Example |
|---|---|---|---|---|
| Overriding | Yes | Yes | The object's real type, at run time | `String sound()` in both classes |
| Overloading | Yes | No (different list) | The arguments, at compile time | `sound()` and `sound(int times)` |
| Static method hiding | Yes | Yes | The variable's declared type | `static String category()` in both classes |
| Field hiding | Yes | Not applicable | The variable's declared type | `String name` in both classes |
A Worked Example
This program shows all four ideas. Save it as Main.java and run it.
class Animal {
String name = "Animal";
static String category() {
return "Animal category";
}
String sound() {
return "some sound";
}
}
class Dog extends Animal {
String name = "Dog";
static String category() {
return "Dog category";
}
@Override
String sound() {
return "Woof";
}
String sound(int times) {
return "Woof x" + times;
}
}
public class Main {
public static void main(String[] args) {
Animal a = new Dog();
Dog d = new Dog();
System.out.println(a.sound()); // Woof
System.out.println(d.sound(2)); // Woof x2
System.out.println(a.category()); // Animal category
System.out.println(d.category()); // Dog category
System.out.println(a.name); // Animal
System.out.println(d.name); // Dog
}
}Look at each line:
a.sound()printsWoof. The variableais declared asAnimal, but the object is aDog. Overriding means Java runs theDogversion.d.sound(2)uses overloading.Doghas a secondsoundmethod with a different parameter list. Java picks it based on the arguments.a.category()printsAnimal category. A static method belongs to the class, not to objects. Java uses the declared type,Animal, soDog's method only hides the original. It does not override it.a.nameprintsAnimal. Field hiding works the same way. The field you read depends on the declared type.
Common Mistakes
A method with the same name but a different parameter list is an overload, not an override. The compiler accepts it without complaint.
// Meant to override sound(), but this is an overload.
@Override
String sound(String word) {
return word;
}The @Override line stops this mistake. It tells the compiler to check that the method really overrides something. If it does not, the compiler reports an error.
> Hiding is not overriding. Hidden static methods and fields do not change when you use a variable of the parent type.
---
Exercises
Exercise
In the example, Animal x = new Dog(); followed by System.out.println(x.name); prints what, and why?
Show answer
It prints Animal. Field hiding depends on the declared type of the variable, which is Animal, so the Animal field is read even though the object is a Dog.
Exercise
What happens if you add @Override String sound(String word) to Dog, and then compile?
Show answer
The compiler reports an error, because sound(String) does not override any method in Animal. The parameter list does not match sound(). Without @Override, the code would compile silently as an overload.