Learn Java
import com.learnjava.oopfundamentals.MethodOverriding;

Method Overriding in Java

@Concept

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.

@HowItWorks
1

Matching signature required

An overriding method needs the exact same name and parameter list as the superclass method it overrides.

2

The @Override annotation

Optional, but has the compiler verify a real matching superclass method exists, catching accidental typos as compile errors.

3

What happens without @Override on a typo

A misspelled override silently becomes an unrelated new method instead, with no compiler warning at all.

4

Overriding replaces behavior for the subclass

Objects of the subclass run the new version; objects of the superclass itself still run the original.

5

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.

@Example
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();
}
}
Output
Meow!
1Cat declares makeSound() with the exact same name and parameter list, none, as Animal's version.
2@Override confirms to the compiler that this is genuinely meant to override an existing method, not create an unrelated new one.
3c.makeSound() runs Cat's overriding version, since c is an actual Cat object.
4The single println call inside Cat's version prints "Meow!" instead of Animal's original generic message.
@UseCases
Customizing inherited behavior for a specific subclass, like Cat's own version of makeSound
Catching a typo'd method name at compile time instead of silently creating an unrelated new method
Overriding toString() so printing a custom object shows something more useful than its default representation
Distinguishing method overriding, across a superclass and subclass, from method overloading within one class

Recommended resourcesAffiliate links · may earn us a commission

Recommended for learners

Structured coding coursesVideo courses that pair well with visual practice — pick one language and go deep.Browse courses Pro code editor & AI assistantWrite, debug and explain code faster while you practice the patterns here.Try it free Classic DSA interview booksThe reference books interviewers themselves learned from — great next to the visualizers.See the books

Practice what you learned

Re-implement each visualized algorithm in Java yourself — building beats watching.