All topics
library
beginner

Method Overriding & @Override

Understand the rules for valid overrides and why @Override is non-negotiable.

A subclass overriding a method replaces the implementation that runs for its objects.

A franchise restaurant: headquarters defines the standard recipe. Each location can customize (override), but must serve the same dish name (same signature).

Key Concepts

1
    class Animal { String speak() { return "..."; } }
2
    class Dog extends Animal {
      @Override
      String speak() { return "Woof"; }
    }
3
The rules a valid override must satisfy.
    same name and exact parameter list
    return type the same, or a subtype
    access the same or wider    protected -> public is fine
    no broader checked exceptions
    the parent method is not static, final or private
4
Always write @Override. It is optional and you should still write it every time:
@Override
5
    @Override
    String speek() { ... }        // compile error: nothing to override
6
Without the annotation, that typo silently declares a brand new method. The code compiles, the parent's version keeps running, and nothing points at the cause.
7
Return types may narrow. This is a covariant return, allowed since Java 5:
8
    class Animal { Animal reproduce() { ... } }
    class Dog extends Animal {
      @Override Dog reproduce() { ... }     // Dog, not Animal
    }
9
Callers holding a Dog then get a Dog back and need no cast.
Dog
10
Exceptions may only narrow too.
    class A { void f() throws IOException { } }
    class B extends A {
      void f() throws FileNotFoundException { }   // narrower, fine
      void f() throws Exception { }               // broader, will not compile
    }
11
Unchecked exceptions are exempt — an override may throw any RuntimeException.
RuntimeException
12
Private and static methods are not overridden. A matching signature in the subclass is a new method, or method hiding for statics, and dispatch then follows the reference type rather than the object.
13
Bridge methods are synthetic methods the compiler generates when generics and covariant returns meet. You never write them, but they appear in stack traces and in reflection output.
14
What the interviewer is probing.1. "@Override is optional. Why use it?" Probing: whether you have been bitten. Stalls: "It is good style." Moves up: a typo silently creates a new method and the parent version keeps running; the annotation turns that into a compile error.
15
2. "Can an overriding method throw a broader checked exception?" Probing: the exception rule. Stalls: "Yes, you can declare what you like." Moves up: no — it may throw the same or narrower checked exceptions, though any unchecked exception is allowed.
16
3. "Can an override return a different type?" Probing: covariant returns. Stalls: "No, it must match exactly." Moves up: it may return a subtype, since Java 5 — so Dog.reproduce() can return Dog where the parent returns Animal.
17
4. "What is a bridge method and where would you see one?" Probing: whether you know what the compiler adds. Stalls: "Never heard of it." Moves up: a synthetic method the compiler generates for covariant returns and erased generics, visible in javap, reflection and stack traces.