Method Overriding in Java
Method overriding lets a subclass provide its own implementation of a method its superclass already defines, replacing that inherited behavior specifically for objects of the subclass — the overriding method must have the exact same name, parameter list, and a compatible return type as the one it overrides. The @Override annotation isn't required for overriding to actually work, but it's strongly recommended, since it makes the compiler verify a genuine matching method actually exists to override, catching a typo'd method name as a compile error instead of silently creating an unrelated new method.
Overriding is entirely different from overloading, despite the similar names. Overriding needs an identical signature and applies specifically to inherited methods across a superclass and subclass relationship, while overloading needs a different parameter list and can happen entirely within one single class.
Matching signature required
An overriding method needs the exact same name and parameter list as the superclass method it overrides.
The @Override annotation
Optional, but has the compiler verify a real matching superclass method exists, catching accidental typos as compile errors.
What happens without @Override on a typo
A misspelled override silently becomes an unrelated new method instead, with no compiler warning at all.
Overriding replaces behavior for the subclass
Objects of the subclass run the new version; objects of the superclass itself still run the original.
Overriding versus overloading
Overriding needs an identical signature across a superclass and subclass pair; overloading needs a different parameter list, and can happen within a single class alone.
class Animal { void makeSound() { System.out.println("Some generic sound"); }} class Cat extends Animal { @Override void makeSound() { System.out.println("Meow!"); }} public class OverrideDemo { public static void main(String[] args) { Cat c = new Cat(); c.makeSound(); }}Recommended resourcesAffiliate links · may earn us a commission
Recommended for learners
Practice what you learned
Re-implement each visualized algorithm in Java yourself — building beats watching.