100% Free Forever
AI-Powered Learning
Industry Expert Content
Certificates & Badges
Learn At Your Own Pace

What is Method Overriding in OOP?

Learn method overriding in OOP: how subclasses redefine inherited methods, dynamic dispatch, and the rules that govern signatures and visibility.

easyQ17 of 226 in Object Oriented Programming Est. time: 5 minsLast updated:
Open Code Lab

Expected Interview Answer

Method overriding is when a subclass provides its own implementation of a method already defined in its superclass, using the same name and signature.

At runtime the JVM (or equivalent runtime) uses dynamic dispatch to invoke the subclass version whenever the call is made through a reference whose actual object is that subclass, even if the reference is declared as the superclass type. The overriding method must have the same parameter list and a compatible return type, and it cannot reduce the visibility of the original method. This is the mechanism that powers runtime polymorphism, letting callers write code against a base type while each subtype supplies specialized behavior.

  • Lets subclasses customize inherited behavior
  • Enables runtime polymorphism via dynamic dispatch
  • Callers can rely on the base-type contract
  • Supports the open-closed principle for extension

AI Mentor Explanation

Every fielding team inherits the base rule "field the ball", but a wicketkeeper overrides it with gloves and stumping technique while a boundary fielder overrides it with a sliding stop and relay throw. The umpire calls "field it" once, and whichever player’s specialized version fires depends on who is actually standing there. That is overriding: same signature, subclass-specific implementation, chosen at the moment of play.

Step-by-Step Explanation

  1. Step 1

    Establish the base method

    Define a method in a superclass with a specific signature and access level.

  2. Step 2

    Redeclare in the subclass

    Use @Override with the identical name, parameters, and a covariant or same return type.

  3. Step 3

    Widen or keep visibility

    The override cannot use a more restrictive access modifier than the base method.

  4. Step 4

    Invoke through the base type

    Call the method on a base-type reference; dynamic dispatch picks the actual object’s version at runtime.

What Interviewer Expects

  • Distinguishing overriding (runtime, inheritance) from overloading (compile-time, same class)
  • Mentioning @Override and signature-matching rules
  • Knowing static and private methods cannot be overridden, only hidden
  • A concrete example showing dynamic dispatch through a base reference

Common Mistakes

  • Confusing overriding with overloading
  • Believing constructors can be overridden
  • Forgetting that access modifiers cannot be narrowed on override
  • Assuming static methods participate in runtime polymorphism

Best Answer (HR Friendly)

Method overriding is when a child class rewrites a behavior it inherited from its parent class, keeping the same method name so existing code still works, but the actual behavior that runs is decided by the real object type at runtime. It is how we let specialized classes customize shared behavior without touching the original code.

Code Example

Overriding a base method
class Employee {
    double bonus() { return 1000; }
}

class Manager extends Employee {
    @Override
    double bonus() { return 5000; }
}

Employee e = new Manager();
System.out.println(e.bonus()); // 5000, resolved at runtime

Follow-up Questions

  • How does @Override help catch bugs at compile time?
  • Why can’t static methods be overridden in Java?
  • What happens if the return type differs on an override attempt?
  • How does the super keyword interact with overriding?

MCQ Practice

1. Method overriding requires the subclass method to have?

Overriding requires an identical name and parameter list so the runtime can substitute the subclass implementation.

2. Which of these cannot be overridden?

Static methods are resolved at compile time by class, so they are hidden, not overridden.

3. An overriding method’s access modifier must be?

You cannot narrow visibility on override; you may keep it the same or widen it.

Flash Cards

What is method overriding?A subclass redefining a superclass method with the same signature, resolved at runtime.

Can static methods be overridden?No — they are hidden, not overridden, since resolution is compile-time by declared type.

What annotation signals an override in Java?@Override, which lets the compiler verify the signature actually matches a superclass method.

What mechanism executes the correct override?Dynamic dispatch, which inspects the object’s actual runtime type.

1 / 4

Continue Learning