what is method overriding in java
Method overriding in Java is when a subclass provides its own implementation of a method that is already defined in its superclass, using the same method name, parameters, and compatible return type. This enables runtime polymorphism so that the version of the method that runs depends on the actual object type, not the reference type.
What Is Method Overriding in Java? (Quick Scoop)
Core idea
When a child class redefines a method it inherits from a parent class, thatās method overriding.
- Same method name.
- Same parameter list (same number and types of parameters).
- Same or covariant (more specific) return type.
- Defined in a subclass that extends a superclass.
At runtime, Java decides which version to call based on the actual object, giving you dynamic (runtime) polymorphism.
Simple example
Imagine a very classic setup:
java
class Animal {
public void move() {
System.out.println("Animals can move");
}
}
class Dog extends Animal {
@Override
public void move() {
System.out.println("Dogs can walk and run");
}
}
public class TestDog {
public static void main(String[] args) {
Animal a = new Animal(); // Animal reference, Animal object
Animal b = new Dog(); // Animal reference, Dog object
a.move(); // calls Animal.move()
b.move(); // calls Dog.move() (overridden method)
}
}
Here, Dog overrides move() from Animal, and the call b.move() uses the
Dog version because the object is actually a Dog.
Key rules you must follow
Some important rules for method overriding in Java:
- Same signature
- Same method name.
- Same parameter types and order.
- Return type must be the same or a subtype (covariant return type).
- Inheritance required
- There must be an inheritance relationship:
class Child extends Parentorclass Sub implements Interface(for implementing interface methods).
- There must be an inheritance relationship:
- Access modifier
- You cannot reduce visibility.
- Example: If the parent method is
public, the overriding method in the child cannot beprotected,default, orprivate.
* You can increase visibility (e.g., `protected` ā `public`).
- Static, final, private
finalinstance methods cannot be overridden.
* `static` methods are not overridden, they are hidden (method hiding).
* `private` methods are not visible to subclasses, so they cannot be overridden.
- Exceptions
- The overriding method cannot throw broader checked exceptions than the superclass method.
- It can throw fewer or narrower checked exceptions, and any unchecked exceptions.
- Use of
@Override- Not mandatory, but highly recommended.
- Helps the compiler catch mistakes if you accidentally change the signature.
Why method overriding matters (real use)
Method overriding is central to objectāoriented design in Java:
- It enables runtime polymorphism: behavior depends on the actual object type.
- It allows you to reuse and customize behavior from parent classes.
- It lets frameworks (like Spring, Android, etc.) call your overridden methods through base-type references.
For example, you might have a base PaymentProcessor class and different
subclasses like CardPaymentProcessor and UPIPaymentProcessor that override
a process() method with their specific logic.
Overriding vs overloading (quick contrast)
- Overriding : same signature, different class (subclass), runtime polymorphism.
- Overloading : same name, different parameter list, same class (or in inheritance chain), compile-time choice.
So if you change only parameters in the same class, youāre overloading; if you redefine the same signature in a subclass, youāre overriding.
Mini FAQ style points
- Can constructors be overridden?
No, constructors are not inherited, so they cannot be overridden.
- Can we call the parent version inside child?
Yes, usingsuper.methodName()to invoke the superclass implementation.
- Is overriding always about classes?
Mostly yes, but you also āoverrideā methods when implementing or reāimplementing interface methods in classes.
Tiny HTML table for quick recap
html
<table>
<tr>
<th>Concept</th>
<th>Method Overriding in Java</th>
</tr>
<tr>
<td>Definition</td>
<td>Subclass provides its own implementation of a method already defined in its superclass, with same signature and compatible return type.</td>
</tr>
<tr>
<td>Purpose</td>
<td>Enable runtime polymorphism and customize inherited behavior.</td>
</tr>
<tr>
<td>Requirements</td>
<td>Inheritance, same method name and parameters, same or covariant return type, access not reduced.</td>
</tr>
<tr>
<td>Not allowed on</td>
<td>final methods, private methods, static methods (they are hidden instead).</td>
</tr>
<tr>
<td>Runtime behavior</td>
<td>Called method depends on actual object type (e.g., Animal ref pointing to Dog object calls Dog's method).</td>
</tr>
</table>
TL;DR: Method overriding in Java is when a subclass replaces a superclass method (same signature, compatible return type) to change or specialize behavior at runtime through polymorphism.
Information gathered from public forums or data available on the internet and portrayed here.