Java OOP · Module 7
Overriding an inherited method
@Override, and the rules that separate overriding from an accidental overload
What you'll be able to do
- Replace a parent's method implementation in a child class
- State the five rules a valid override must satisfy
- Explain what `@Override` actually does for you
- Spot an accidental overload that was meant to be an override
Method Overriding
The child has a better answer
`Animal.sound()` can only say 'makes a sound'. A Dog knows better — and it does not need a new method name to say so.
Method overriding = a subclass declares a method with the *same signature* as one it inherited, and provides a different body. From then on, that object uses the child's version.
Why this matters so much: the *caller does not change*. Everyone keeps calling sound(). The behaviour that runs depends on what the object actually is. That single idea is what makes polymorphism (Module 8), abstract classes (Module 10) and interfaces (Module 11) work — all three are built on overriding.
The same job description, different specialists
A hospital's job description says every doctor must be able to treat(patient). The cardiologist, the dentist and the paediatrician all keep that exact job title — and each does something completely different. Reception never needs to know which one it is: it just says 'treat this patient'. Overriding is the specialist rewriting the *how* while keeping the *what*.
A correct override
super.sound().Add a `Cow` that overrides `sound()` with 'Moo', then make a Cow speak. You never invent a new method name.
Always write @Override
It is optional for the compiler but priceless for you. Misspell sound as Sound, or write sound(String s) by accident, and without the annotation Java happily creates a brand-new method that nobody ever calls — a silent bug that can take hours to find. With @Override it is a compile error in one second.
Prove it — coding challenges
Overriding Rules
The rules the compiler checks
'Same method' is a precise idea in Java, not a feeling. Five rules define it.
Rules for a valid override
| Rule | Must be | If you break it |
|---|---|---|
| Method name | exactly the same | you created an unrelated method |
| Parameter list | exactly the same (types and order) | you created an overload, not an override |
| Return type | same, or a subclass of it (covariant) | compile error: incompatible return type |
| Access level | same or wider | compile error: attempting to assign weaker access privileges |
| Target method | not static, not final, not private | you hide or shadow it instead of overriding it |
Notice the asymmetry in rule 4: a `protected` method may be overridden as `public` (widening is fine), but a `public` method can never become `protected` — that would break every existing caller.
Override or overload? The parameter list decides
Override ✅
same signature
- Replaces the inherited behaviour
- Chosen at runtime by the object's real type
animal.sound()prints Woof
Accidental overload ❌
different parameters
- A brand-new method; the parent's
sound()is still inherited dog.sound()still prints the generic message@Overrideon it would be a compile error — which is why you add it
`final` and `static` are the two escape hatches
final void pay() forbids overriding — useful when a method enforces something that must never change. A static method belongs to the class, not the object, so redeclaring it in a child *hides* it (resolved by the reference type at compile time) rather than overriding it. Interview trap: static methods are never polymorphic.
Common mistakes
@Override void sound(String volume) { } // parent declares sound()Overriding a `public` method as `protected`.Writing `Sound()` or `sound2()` because 'the name was taken'.Lesson playground
The parent's describe() calls draw(). Override draw() in Square and watch describe() change behaviour without being touched. Now add a Triangle.
Check understanding
Quiz
Question 1 of 5
Engineer overrides one method and inherits the other. What is printed?
Summary
- Overriding = same name, same parameter list, compatible return type, access not narrowed.
- `@Override` turns a silent typo into an instant compile error — always write it.
- Different parameters make an overload, not an override.
- `static`, `final` and `private` methods cannot be overridden.