Sub topic 3
Overriding a Method
A subclass gets every method its superclass has. Sometimes the inherited version is wrong for the subclass. A generic animal might make a vague sound, but a dog should bark. Overriding lets the subclass replace that inherited behavior with its own.
What Overriding Means
Overriding is writing a method in a subclass that has the same name and the same parameter list as a method in its superclass. The parameter list is the number, order, and types of the values a method takes. When both match, the subclass version replaces the inherited one for objects of that subclass.
The superclass method is still there. Objects of the superclass keep using it, and so do subclasses that do not override it.
A Worked Example
Here, Animal has a sound() method. Dog and Cat each provide their own version.
class Animal {
String sound() {
return "...";
}
}
class Dog extends Animal {
@Override
String sound() {
return "Woof";
}
}
class Cat extends Animal {
@Override
String sound() {
return "Meow";
}
}
public class Main {
public static void main(String[] args) {
Animal[] animals = { new Dog(), new Cat(), new Animal() };
for (Animal a : animals) {
System.out.println(a.sound());
}
}
}Save this as Main.java, then run javac Main.java and java Main. The output is:
Woof
Meow
...Look at what happened. Every element of the array is declared as Animal. Even so, the dog printed Woof and the cat printed Meow. Java used the method from each object's actual class. The plain Animal object has no override, so it used the original method and printed ....
How the Pieces Fit
Checklist for an Override
| Part of the method | Must it match the superclass? |
|---|---|
| Method name | Yes, exactly, including capital letters |
| Parameter list | Yes, same number, order, and types |
| Method body | No, this is the part you change |
If the name or the parameters differ, you have not overridden anything. You have written a new, separate method, and the old one is still inherited unchanged. For example, String Sound() with a capital S in Dog would not replace sound(). A dog would still print ....
Return types and access levels such as public also follow rules. A later chapter covers them. For now, keep them the same as in the superclass.
Why This Matters
Overriding lets you write code that works with the general type, Animal, while each object still behaves in its own way. The loop in main does not need to check whether each animal is a dog or a cat. It calls sound(), and the right version runs. Later chapters explain how Java chooses that version while the program runs.
Exercise
You add class Cow extends Animal { String sounds() { return "Moo"; } } and put new Cow() in the array. What does the loop print for the cow, and why?
Show answer
It prints "...". The method is named sounds(), not sound(), so it does not override anything. Cow still inherits Animal's sound(), which returns "...".
Exercise
Write a Duck class that overrides sound() so that it returns "Quack".
Show answer
class Duck extends Animal { @Override String sound() { return "Quack"; } }