Java Method Overriding
Method overriding happens when a child class writes its own version of a method that the parent class already has. The method name, parameters, and return type stay the same. Only the body changes.
A Family Recipe Example
A grandmother makes soup with a plain recipe. Her grandson inherits the recipe and adds chili. Guests who order "family soup" from the grandson taste the spicy version. The recipe name stays the same and the result differs. Java works the same way.
Diagram: Parent and Child Versions
+------------------------+
| Animal |
| sound() -> "..." |
+-----------+------------+
|
+-------+--------+
| |
+---+-----+ +----+----+
| Dog | | Cat |
| sound() | | sound() |
| "Woof" | | "Meow" |
+---------+ +---------+
Basic Example
class Animal {
void sound() {
System.out.println("Some sound");
}
}
class Dog extends Animal {
@Override
void sound() {
System.out.println("Woof");
}
}
class Cat extends Animal {
@Override
void sound() {
System.out.println("Meow");
}
}
Runtime Choice
Java decides which version to run while the program runs. The decision depends on the real object and not on the variable type. This behavior is called dynamic method dispatch.
Animal pet = new Dog();
pet.sound(); // Woof
pet = new Cat();
pet.sound(); // Meow
The variable pet has the type Animal. The object inside decides the output.
Rules of Overriding
| Rule | Meaning |
|---|---|
| Same name | The child uses the parent method name |
| Same parameters | The parameter list matches exactly |
| Compatible return type | Same type, or a child type of the parent return type |
| Access not weaker | A public parent method cannot become private in the child |
| Inheritance needed | The method must come from a parent class |
What You Cannot Override
- final methods
- static methods, which belong to the class and get hidden instead
- private methods, which the child cannot see
- constructors
The Override Annotation
Write @Override above the child method. The compiler checks that a matching parent method exists. A spelling error triggers a clear message instead of a silent bug.
class Dog extends Animal {
@Override
void sond() { } // Error: method does not override a parent method
}
Calling the Parent Version
Use super inside the child method to run the parent code as part of the new code.
class Bird extends Animal {
@Override
void sound() {
super.sound();
System.out.println("Tweet");
}
}
Real Example: Payment Types
A shop accepts cash and cards. Each payment type calculates its fee in a different way. The parent method sets a default. Each child changes the rule.
class Payment {
double fee(double amount) {
return 0;
}
}
class CardPayment extends Payment {
@Override
double fee(double amount) {
return amount * 0.02;
}
}
Overriding and Overloading Compared
| Feature | Overloading | Overriding |
|---|---|---|
| Where | Same class | Parent and child |
| Parameters | Must differ | Must match |
| Decided | At compile time | At run time |
Common Mistakes
- Changing a parameter type and creating an overload by accident
- Skipping @Override and missing spelling errors
- Reducing access, such as public to default
Covariant Return Types
An overriding method may return a child type of the parent return type. This feature is called a covariant return. A parent method that returns Animal can become a child method that returns Dog.
class Shelter {
Animal adopt() { return new Animal(); }
}
class DogShelter extends Shelter {
@Override
Dog adopt() { return new Dog(); }
}
Quick Summary
- Overriding replaces a parent method inside a child class
- The real object picks the version at run time
- Use @Override every time
