# Method Overriding

> Method overriding is when a subclass defines a method with the same name, parameters and return type as one in its superclass, replacing the inherited behavior with its own. It is the core mechanism of runtime polymorphism, because the version that runs is chosen during execution from the object's real type, not the variable's declared type.

Canonical: https://devsunite.com/resources/core-subjects/oop/method-overriding
Last updated: 2026-11-22

Subject: OOP · Category: Polymorphism · 3 min read

Method overriding is when a subclass defines a method with the exact same name, parameters, and return type as one already defined in its superclass, replacing the inherited behavior with its own. It's the core mechanism behind runtime polymorphism: which version actually runs is decided while the program is executing, based on the real type of the object, not the type of the variable holding it.

## How it works

For a method to genuinely override (rather than accidentally overload or hide) a superclass method, the signature must match exactly, same name, same parameter types, same return type (or a covariant subtype of it). Most languages provide a way to make this intent explicit and catch mistakes at compile time, `@Override` in Java, `override` in C++11 and later.

## A worked example

```java
class Animal {
    String makeSound() {
        return "Some generic animal sound";
    }
}

class Dog extends Animal {
    @Override
    String makeSound() {
        return "Woof!";
    }
}

class Cat extends Animal {
    @Override
    String makeSound() {
        return "Meow!";
    }
}

public class Main {
    public static void main(String[] args) {
        Animal[] animals = { new Dog(), new Cat(), new Animal() };

        for (Animal a : animals) {
            System.out.println(a.makeSound());
        }
        // prints: Woof!  Meow!  Some generic animal sound
    }
}
```

Every element of the array is declared as type `Animal`, but each one prints its own, correct, overridden sound. The decision of which `makeSound()` actually runs happens at runtime, based on whether the real object is a `Dog`, `Cat`, or plain `Animal`.

## Where this matters in real systems

Almost every framework that lets you "hook into" its behavior relies on overriding. In Android app development, you override lifecycle methods like `onCreate()` or `onResume()` on an `Activity` base class to run your own code at the right moment, the framework calls the method by name on your object, and your overridden version runs instead of (or in addition to, via `super.onCreate()`) the base class's default behavior. Custom exception classes commonly override `toString()` or `getMessage()` to provide more specific, useful error output than the generic base `Exception` class would.

## What interviewers actually ask about method overriding

- **"What's required for a method to properly override a superclass method?"** The exact same method name, parameter list, and return type (or a covariant subtype of the return type), and it can't reduce the method's accessibility (can't override a public method with a private one).
- **"What does `@Override` (or `override` in C++) actually do?"** It's not strictly required for overriding to work, but it tells the compiler to verify the method genuinely does override something in a superclass, catching accidental typos or signature mismatches as compile errors instead of silent bugs.
- **"Can you call the superclass's version of an overridden method?"** Yes, using `super.methodName()` (Java) or `Base::methodName()` (C++) from inside the overriding method.
- **"What's the difference between overriding and hiding a static method?"** Only instance methods can be truly overridden with runtime dispatch; if a subclass defines a static method with the same signature as the superclass's static method, it "hides" rather than overrides it, and which one runs is decided at compile time based on the reference type, not the real object.

## Key takeaways

- Overriding lets a subclass replace a superclass's method with its own implementation, using the exact same signature.
- Which overridden version runs is decided at runtime, based on the object's real type.
- `@Override`/`override` annotations help the compiler catch mistakes, though they aren't strictly required.
- Framework "hook" methods (lifecycle callbacks, event handlers) are a common real-world use of overriding.

## Related

- [Previous: Function Overloading](https://devsunite.com/resources/core-subjects/oop/function-overloading)
- [Next: Virtual Functions and Inline Virtual Functions](https://devsunite.com/resources/core-subjects/oop/virtual-functions-and-inline-virtual-functions)
- [More in Polymorphism: Overview of Polymorphism](https://devsunite.com/resources/core-subjects/oop/overview-of-polymorphism)
- [More in Polymorphism: Abstract Classes and Pure Virtual Functions](https://devsunite.com/resources/core-subjects/oop/abstract-classes-and-pure-virtual-functions)
- [All 72 OOP interview questions](https://devsunite.com/resources/core-subjects/oop)
