What is Method Overriding in Java?
Method overriding in Java is a feature where a child class provides its own implementation of a method that already exists in the parent class.
In simple words:
Method overriding allows a subclass to modify or redefine the behavior of a parent class method.
Why Method Overriding is Important?
Method overriding helps achieve:
- Runtime Polymorphism
- Dynamic Method Dispatch
- Flexible application design
- Code extensibility
- Reusable architecture
Real-Time Example
Think about a payment application:
- UPI Payment
- Credit Card Payment
- Net Banking Payment
All payment systems perform:
makePayment()
But implementation differs for each payment type.
Method Overriding Flow Diagram
Parent Class
|
v
makePayment()
|
+-------> UPI Payment
|
+-------> Credit Card Payment
|
+-------> Net Banking Payment
Basic Example of Method Overriding
class Animal {
void sound() {
System.out.println("Animal makes sound");
}
}
class Dog extends Animal {
@Override
void sound() {
System.out.println("Dog barks");
}
}
Explanation
Here:
- Dog class inherits Animal class
- Dog provides its own implementation of sound()
This is method overriding.
Method Overriding Execution Flow
Parent Reference
|
v
Child Object Created
|
v
Overridden Method Called
|
v
Child Method Executes
Runtime Polymorphism
Method overriding is also called:
Runtime Polymorphism
Why?
Because method selection happens during runtime based on object creation.
Example
Animal a = new Dog(); a.sound();
Output
Dog barks
Why Child Method Executes?
Because object type determines method execution during runtime.
Rules of Method Overriding
- Method name must be same
- Parameters must be same
- Inheritance is required
- Return type must be same or covariant
- Access modifier cannot be more restrictive
Valid Method Overriding Example
class Vehicle {
void start() {
System.out.println("Vehicle Starting");
}
}
class Car extends Vehicle {
void start() {
System.out.println("Car Starting");
}
}
Invalid Method Overriding Example
class Parent {
void show(int a) {
}
}
class Child extends Parent {
void show() {
}
}
Why Invalid?
Parameters differ. This becomes method overloading instead of overriding.
@Override Annotation
Java provides:
@Override
to indicate overridden methods.
Example
@Override
void sound() {
System.out.println("Dog barks");
}
Benefits of @Override
- Improves readability
- Compiler validation
- Reduces mistakes
Dynamic Method Dispatch
Java resolves overridden methods during runtime.
Dynamic Dispatch Flow
Reference Type → Parent
Object Type → Child
|
v
Runtime decides method execution
Method Overriding in Banking Systems
Banking applications use overriding for:
- Transaction processing
- Payment gateways
- Notification systems
- Fraud detection engines
Example
processTransaction()
Different banks implement different processing logic.
Method Overriding in E-Commerce Systems
E-commerce applications override methods for:
- Shipping calculation
- Discount engines
- Payment processing
- Search ranking
Method Overriding in Spring Boot
Spring Boot and enterprise frameworks heavily use method overriding.
Examples
- Custom exception handling
- Security configuration
- Bean customization
- REST controller extensions
Spring Boot Example
@Override
protected void configure(
HttpSecurity http
) throws Exception {
}
Method Overriding vs Method Overloading
| Feature | Method Overriding | Method Overloading |
|---|---|---|
| Occurs In | Parent-Child Class | Same Class |
| Parameters | Must be same | Must differ |
| Polymorphism Type | Runtime | Compile-Time |
| Inheritance Required | Yes | No |
Can Static Methods be Overridden?
No. Static methods belong to the class, not objects.
Example
class Parent {
static void test() {
}
}
class Child extends Parent {
static void test() {
}
}
This is NOT Overriding
This is called:
- Method Hiding
Can Final Methods be Overridden?
No.
Example
final void display() {
}
Child class cannot override it.
Can Private Methods be Overridden?
No.
Private methods are not inherited.
Covariant Return Type
Java allows child methods to return subclass types.
Example
class Animal {
}
class Dog extends Animal {
}
class Parent {
Animal getAnimal() {
return new Animal();
}
}
class Child extends Parent {
Dog getAnimal() {
return new Dog();
}
}
Method Overriding Internal Working
Parent Reference
|
v
Object Created at Runtime
|
v
JVM Checks Actual Object Type
|
v
Overridden Method Executes
Advantages of Method Overriding
- Supports runtime polymorphism
- Improves flexibility
- Enables dynamic behavior
- Supports extensibility
- Encourages reusable architecture
Disadvantages
- Complex inheritance structures may reduce readability
- Runtime resolution has slight overhead
Common Interview Mistake
Many developers think changing parameters creates overriding.
Actually:
- Changing parameters creates overloading.
- Overriding requires same method signature.
Best Practices
- Use @Override annotation
- Follow proper inheritance design
- Avoid deep inheritance hierarchies
- Keep overridden logic meaningful
- Use polymorphism effectively
Method Overriding in Microservices
Modern microservices architectures use overriding for:
- Custom authentication
- Logging frameworks
- Service customization
- Cloud configuration
Realtime Enterprise Example
Parent Class
NotificationService
Child Implementations
- EmailNotificationService
- SMSNotificationService
- PushNotificationService
Each overrides:
sendNotification()
Related Learning Topics
- What is Method Overloading in Java
- Difference Between Overloading and Overriding
- What is Inheritance in Java
- What is Spring Boot
- What are Microservices
Professional Interview Answer
Method overriding in Java is a feature where a child class provides its own implementation of a method already defined in the parent class using the same method signature. It enables runtime polymorphism because JVM determines the actual method to execute during runtime based on object type. Method overriding improves flexibility, extensibility, and dynamic behavior in object-oriented applications. It is heavily used in enterprise systems, Spring Boot applications, microservices architectures, payment systems, notification frameworks, and cloud-native platforms.
Frequently Asked Questions
What is method overriding in Java?
Method overriding allows a child class to redefine a parent class method using the same method signature.
Why is method overriding called runtime polymorphism?
Because JVM decides method execution during runtime.
Can static methods be overridden?
No, static methods cannot be overridden.
Can final methods be overridden?
No, final methods cannot be overridden.
Why use @Override annotation?
It improves readability and helps compiler validation.