Introduction
Method Overriding is one of the most important concepts in Object-Oriented Programming (OOP) and forms the foundation of Runtime Polymorphism in Java. It allows a subclass to provide its own implementation of a method that is already defined in its superclass. This enables child classes to customize or extend inherited behavior without modifying the parent class.
For example, consider an Animal class with a sound() method. A Dog class can override this method to produce a barking sound, while a Cat class can override it to produce a meowing sound. Although the method name remains the same, the behavior changes depending on the actual object.
Method overriding supports the “one interface, many implementations” principle.
In Java, method overriding works together with inheritance and dynamic method dispatch, allowing the Java Virtual Machine (JVM) to determine which method implementation should execute during runtime.
What is Method Overriding?
Method Overriding occurs when a subclass provides its own implementation of a method that already exists in the superclass.
The overriding method must have the same method name, same parameter list, and a compatible return type.
Definition:
Method overriding is the process in which a child class provides its own implementation of an inherited method using the same method signature.
Why Do We Need Method Overriding?
Without method overriding, every subclass would be forced to use the parent’s implementation, even when different behavior is required.
Example:
class Animal {
void sound() {
System.out.println("Animal Sound");
}
}
Different animals produce different sounds.
Using overriding:
class Dog extends Animal {
@Override
void sound() {
System.out.println("Dog Barks");
}
}
Now each subclass can define behavior specific to itself.
Syntax
class Parent {
void display()
}
}
class Child extends Parent {
@Override
void display() {
}
}
The @Override annotation tells the compiler that the method is intended to override a superclass method.
Basic Method Overriding Example
class Animal {
void sound() {
System.out.println("Animal Sound");
}
}
class Dog extends Animal {
@Override
void sound() {
System.out.println("Dog Barks");
}
}
public class Main {
public static void main(String[] args) {
Dog dog = new Dog();
dog.sound();
}
}
Output
The child class replaces the inherited implementation with its own.
Runtime Method Dispatch
Runtime method dispatch is the mechanism that allows Java to choose the appropriate overridden method during execution.
Example:
class Animal {
void sound() {
System.out.println("Animal Sound");
}
}
class Dog extends Animal {
@Override
void sound() {
System.out.println("Dog Barks");
}
}
public class Main {
public static void main(String[] args) {
Animal animal = new Dog();
animal.sound();
}
}
Output
Although the reference type is Animal, the JVM calls the Dog implementation because the actual object is of type Dog.
Rules of Method Overriding
Method overriding follows these rules:
- The method name must be the same.
- The parameter list must be the same.
- The return type must be the same or covariant.
- The subclass method cannot have a more restrictive access modifier.
- Static methods cannot be overridden (they are hidden).
- Final methods cannot be overridden.
- Private methods are not inherited and therefore cannot be overridden.
- Constructors cannot be overridden.
- The overriding method may throw the same exception or a narrower checked exception.
The @Override Annotation
The @Override annotation is optional but strongly recommended.
Example:
class Animal {
void sound() {
}
}
class Dog extends Animal {
@Override
void sound() {
System.out.println("Dog Barks");
}
}
Benefits of @Override
- Helps detect mistakes at compile time.
- Improves code readability.
- Makes the programmer’s intention clear.
- Prevents accidental overloading instead of overriding.
Covariant Return Type
Java allows an overriding method to return a subclass of the original return type.
Example:
class Animal {
}
class Dog extends Animal
class Factory
Animal create() {
return new Animal()
}
}
class DogFactory extends Factory {
@Override
Dog create() {
return new Dog();
}
}
Returning a more specific type is called a covariant return type.
Methods That Cannot Be Overridden
The following members cannot be overridden:
| Member | Can Be Overridden? |
|---|---|
| Private Method | No |
| Final Method | No |
| Static Method | No(method hiding) |
| Constructor | No |
Example:
class Parent {
final void display() {
}
}
class Child extends Parent {
// Compilation Error
}
Method Overriding vs Method Overloading
| Method Overriding | Method Overloading |
|---|---|
| Same class | Parent and child classes |
| Same method name | Same method name |
| Different parameter list | Same parameter list |
| Compile-time polymorphism | Runtime polymorphism |
| Compiler decides | JVM decides |
| Inheritance not required | Inheritance required |
Runtime Method Dispatch Flow
Animal animal = new Dog();
│
▼
Reference Type = Animal
│
▼
Actual Object = Dog
│
▼
JVM Executes Dog's sound()
The JVM chooses the method implementation based on the object’s actual type.
Real-Life Example
Imagine a Payment Gateway.
class Payment {
void pay() {
System.out.println("Processing Payment");
}
}
class CreditCardPayment extends Payment {
@Override
void pay() {
System.out.println("Paid Using Credit Card");
}
}
class UPIPayment extends Payment {
@Override
void pay() {
System.out.println("Paid Using UPI");
}
}
public class Main {
public static void main(String[] args) {
Payment payment;
payment = new CreditCardPayment();
payment.pay();
payment = new UPIPayment();
payment.pay();
}
}
Output
Paid Using UPI
The same method call performs different actions depending on the actual payment type.
Advantages of Method Overriding
Method overriding offers several benefits:
- Supports runtime polymorphism.
- Promotes code reusability.
- Improves application flexibility.
- Enables specialized behavior in subclasses.
- Simplifies application maintenance.
- Encourages loose coupling.
- Supports framework and API extensibility.
- Improves object-oriented design.
Best Practices
Follow these best practices when using method overriding:
- Always use the @Override annotation.
- Override methods only when behavior truly changes.
- Keep overridden methods consistent with the parent class contract.
- Avoid changing expected behavior unexpectedly.
- Use meaningful class hierarchies.
- Prefer composition over inheritance when an “is-a” relationship does not exist.
- Keep overridden methods focused and easy to understand.
Common Mistakes
Beginners often make these mistakes:
- Changing the parameter list and assuming the method is overridden.
- Forgetting the @Override annotation.
- Trying to override a final method.
- Attempting to override private methods.
- Confusing static method hiding with overriding.
- Assuming constructors can be overridden.
- Using a more restrictive access modifier in the child method.
- Confusing overriding with overloading.
Conclusion
Method Overriding is a core feature of Java that enables subclasses to provide specialized implementations of inherited methods. It forms the basis of Runtime Polymorphism, allowing the JVM to execute the appropriate method based on the actual object at runtime.
By understanding the rules of overriding, runtime method dispatch, the @Override annotation, and covariant return types, you can write flexible, reusable, and maintainable object-oriented applications. Mastering method overriding is essential before moving on to advanced topics such as Runtime Polymorphism, the super keyword, Abstraction, Interfaces, and Java design patterns.
Java Method Overriding – Interview Questions
Q 1: What is method overriding?
Q 2: When is overriding resolved?
Q 3: Which keyword prevents overriding?
Q 4: Can static methods be overridden?
Q 5: Why is method overriding used?
Java Method Overriding – Objective Questions (MCQs)
Q1. What is required for a method to be overridden in Java?
Q2. Which of the following statements about method overriding is TRUE?
Q3. What happens if a subclass method does not override a superclass method?
Q4. Which annotation is commonly used to indicate method overriding in Java?
Q5. Can a subclass override a superclass method and change its access modifier to more restrictive?