Method Overriding means redefining a parent class method in the child class with the same name, return type and parameters. Used to implement runtime polymorphism and customize behavior.

Example

class Animal {
    void makeSound() {
        System.out.println("Default sound");
    }
}
 
class Dog extends Animal {
    @Override
    void makeSound() {
        System.out.println("Bark");
    }
}

Rules

RuleisRequired
Same method nameβœ…
Same parameter listβœ…
Same or covariant return typeβœ…
Must be inheritedβœ…
Use @Override annotationβœ… (recommended)
Access modifier can’t be more restrictiveβœ…
  • Only instance methods can be overridden - not static methods.
  • Private methods are not inherited, so can’t be overridden.
  • Constructors can’t be overridden.

Comparison

Overriding vs Overloading

FeatureOverloadingOverriding
Occurs InSame classSubclass (inheritance)
ParamsMust be differentMust be same
Return TypeCan differMust be same (or covariant)
Polymorphism TypeCompile-timeRuntime

Tip

Think β€œcustom behavior in subclasses” when you hear overriding.
Use @Override to catch mistakes early - it’s not required, but highly recommended.


Parent: _OOP Concepts