Sub topic 12
Override Instance Methods
An instance method is a method called on an object. Overriding lets a child class provide its own version of an instance method from a parent class.
A class is a plan for making objects. A child class extends a parent class, which means it can use suitable methods from the parent and add or change behavior. This relationship is called inheritance.
Overriding is useful when related objects need to respond differently to the same method call.
A worked example
Save this code in a file named AnimalDemo.java:
class Animal {
public String sound() {
return "Some sound";
}
public String describe() {
return "Animal: " + sound();
}
}
class Dog extends Animal {
@Override
public String sound() {
return "Woof";
}
}
public class AnimalDemo {
public static void main(String[] args) {
Animal animal = new Animal();
Animal pet = new Dog();
System.out.println(animal.sound());
System.out.println(pet.sound());
System.out.println(pet.describe());
}
}Run it from the folder containing the file:
javac AnimalDemo.java
java AnimalDemoThe output is:
Some sound
Woof
Animal: WoofDog extends Animal makes Dog a child of Animal. The sound method in Dog overrides the one in Animal: it has the same name and parameter types, but provides different behavior.
Dog does not declare a describe method, so it inherits that method from Animal. Inheritance gives Dog access to that parent behavior.
How Java chooses a method
Look at this line:
Animal pet = new Dog();The declared type of pet is Animal, but the actual object is a Dog. The variable holds a reference, which is a value Java uses to find the object.
When the program runs, Java chooses an overridden instance method based on the actual object. This choice is called dynamic dispatch. Since pet refers to a Dog, pet.sound() calls Dog.sound() and prints Woof.
The call to pet.describe() is more subtle:
- Java finds the inherited
describemethod inAnimal. describecallssound()on the sameDogobject.- Java chooses
Dog.sound(), so the result isAnimal: Woof.
The declared type still matters. Through pet, Java lets you call methods available on Animal. If Dog had an extra method that Animal did not have, pet could not call it directly.
Rules and common mistakes
An annotation is a marker that gives Java extra information about code. Put @Override above an overriding method. It asks the compiler to check that the method really overrides one from a parent class. The compiler is the tool that checks and translates your Java code.
For example, if you write sounds instead of sound, @Override helps catch the mistake.
Keep these rules in mind:
- Use the same method name and parameter types. A parameter is a named input in a method declaration.
- Use the same return type, or a compatible narrower type. In the example, both methods return
String. - Do not make the overriding method less accessible. For example, an overriding method cannot change a parent’s
publicmethod toprivate. - You cannot override a
finalmethod. Afinalmethod is marked as not changeable by child classes. - A
privatemethod belongs only to its own class and is not inherited for overriding. Astaticmethod belongs to a class, not an object; a child can hide it rather than override it.
Overriding is different from overloading. Overloading uses the same method name with different parameter types or counts. Changing a parameter does not override the original method.
Call the parent version
Inside an overriding method, super lets you call the parent version. For example, replace the body of Dog.sound() with:
return super.sound() + " and woof";Now pet.sound() returns Some sound and woof. This is useful when the child wants to add to the parent’s behavior rather than replace it completely.
Exercise
In the original example, what does pet.describe() print, and why?
Show answer
It prints Animal: Woof. Dog inherits describe from Animal, but describe calls sound on the Dog object, so Java uses Dog.sound().