Method Overloading vs Overriding in Java
Understand method overloading vs overriding in Java: compile-time vs runtime polymorphism, signature rules, covariant returns, code examples and interview Q&A.
Expected Interview Answer
Method overloading defines several methods with the same name but different parameter lists in the same class and is resolved at compile time, while method overriding redefines an inherited superclass method with the same signature in a subclass and is resolved at runtime.
Overloading is compile-time (static) polymorphism: the compiler chooses the matching method by argument types and count, and the return type alone cannot distinguish overloads. Overriding is runtime (dynamic) polymorphism: the JVM dispatches to the actual object's implementation, the signature must match, the return type must be the same or covariant, and the overriding method cannot weaken access or throw broader checked exceptions. Static, final, and private methods cannot be overridden.
- Overloading improves readability with intuitive method names
- Overriding enables runtime polymorphism and extensibility
- Overloading offers flexible parameter options
- Overriding lets subclasses specialize behaviour
- Together they express both static and dynamic polymorphism
AI Mentor Explanation
Overloading is one word 'bowl' meaning several deliveries chosen by grip and context — bowl a bouncer, bowl a yorker — decided as you set the field before the ball. Overriding is a young spinner inheriting the captain's standard field but redefining how they actually bowl it in the moment, the real action decided live at delivery.
Step-by-Step Explanation
Step 1
Compare the signatures
Overloading changes the parameter list within one class; overriding keeps the exact same signature across a superclass and subclass.
Step 2
Identify the binding time
Overloading is resolved by the compiler (static); overriding is resolved by the JVM at runtime (dynamic).
Step 3
Check the return type rules
Return type alone cannot overload; an override must return the same type or a covariant subtype.
Step 4
Respect override constraints
An override cannot reduce visibility or throw broader checked exceptions, and static/final/private methods can't be overridden.
Step 5
Use @Override to verify
Annotate overrides so the compiler confirms you truly override rather than accidentally overload.
What Interviewer Expects
- Compile-time vs runtime polymorphism distinction
- Signature and return-type rules for each
- Knowing return type alone cannot overload
- Override constraints on access, exceptions, static/final/private
- Correct use of the @Override annotation
Common Mistakes
- Thinking changing only the return type overloads a method
- Believing static methods can be overridden (they are hidden, not overridden)
- Saying overriding is resolved at compile time
- Confusing overloading with overriding when parameter lists differ across parent and child
Best Answer (HR Friendly)
“Overloading is having several methods with the same name in one class that differ by their inputs, and the compiler picks the right one. Overriding is a child class replacing a method it inherited from its parent, and the program decides which version to run while it is actually running.”
Code Example
class Calculator {
int add(int a, int b) { return a + b; }
double add(double a, double b) { return a + b; } // different params
int add(int a, int b, int c) { return a + b + c; } // different count
}
public class Demo {
public static void main(String[] args) {
Calculator c = new Calculator();
System.out.println(c.add(2, 3)); // int version
System.out.println(c.add(2.5, 3.5)); // double version
System.out.println(c.add(1, 2, 3)); // three-arg version
}
}class Animal {
String sound() { return "Some sound"; }
}
class Dog extends Animal {
@Override
String sound() { return "Woof"; } // same signature, redefined
}
public class Demo {
public static void main(String[] args) {
Animal a = new Dog(); // reference type Animal
System.out.println(a.sound()); // prints Woof — resolved at runtime
}
}Follow-up Questions
- Why can't the return type alone distinguish overloaded methods?
- What is covariant return type in overriding?
- Can you override a static method, and what actually happens?
- How do access modifiers and checked exceptions constrain an override?
- How does the @Override annotation help catch bugs?
MCQ Practice
1. Method overloading in Java is a form of which polymorphism?
Overloading is resolved by the compiler using argument types and count, making it compile-time (static) polymorphism.
2. Which change alone is NOT enough to overload a method?
Changing only the return type does not create a valid overload; the parameter list must differ.
3. An overriding method in a subclass may return what relative to the parent method?
Overriding permits the same return type or a covariant (subtype) return type; unrelated types break the override.
Flash Cards
Overloading vs overriding in one line? — Overloading: same name, different params, compile-time. Overriding: same signature in a subclass, runtime.
Can return type alone overload? — No — the parameter list must differ; return type alone is not enough.
Which polymorphism is each? — Overloading is static (compile-time); overriding is dynamic (runtime) polymorphism.
Can static methods be overridden? — No — they are hidden, not overridden; dispatch is by reference type, not object.