Method Overloading vs Overriding in Java (Table + Rules)
Overloading = same name, different parameters, same class, compile-time. Overriding = same signature, child class, runtime. Full rules and code proof.
The one-line answer
Overloading example (compile-time polymorphism)
class Calculator {
int add(int a, int b) { return a + b; }
int add(int a, int b, int c) { return a + b + c; } // more params
double add(double a, double b) { return a + b; } // diff types
}
Calculator c = new Calculator();
System.out.println(c.add(2, 3));
System.out.println(c.add(2, 3, 4));
System.out.println(c.add(2.5, 3.5));
The compiler picks the right version by looking at the arguments — decided before the program even runs.
int add(int,int) and double add(int,int) together is a compile error. Parameters must differ in number, type, or order.Overriding example (runtime polymorphism)
class Animal {
void sound() { System.out.println("Some generic sound"); }
}
class Dog extends Animal {
@Override
void sound() { System.out.println("Bark!"); } // same signature
}
Animal a = new Dog(); // parent reference, child object
a.sound(); // which version runs?
Even though the reference type is Animal, the object's version runs — the JVM decides at runtime by looking at the actual object. This is runtime (dynamic) polymorphism, the backbone of frameworks like Spring.
Comparison table
| Point | Overloading | Overriding |
|---|---|---|
| Parameters | Must be different | Must be identical |
| Classes involved | Same class (or child adding) | Parent + child required |
| Resolved | Compile time (static) | Runtime (dynamic) |
| Return type | Can differ freely | Same or covariant |
| Access modifier | Any | Cannot be more restrictive |
| static/final/private | Can be overloaded | Cannot be overridden |
| Annotation | — | @Override (recommended) |
Overriding rules interviewers test
- Same signature: name + parameters must match exactly.
- Access cannot shrink: a public parent method cannot become protected in the child.
- static methods: cannot be overridden — a same-signature static in the child is method hiding, resolved by reference type, not object.
- final and private methods: cannot be overridden at all.
- @Override annotation: optional but always use it — the compiler then catches signature typos that would silently create an overload instead.
One-line answer
Overloading example (compile-time polymorphism)
class Calculator {
int add(int a, int b) { return a + b; }
int add(int a, int b, int c) { return a + b + c; } // ज़्यादा params
double add(double a, double b) { return a + b; } // अलग types
}
Calculator c = new Calculator();
System.out.println(c.add(2, 3));
System.out.println(c.add(2, 3, 4));
System.out.println(c.add(2.5, 3.5));
Compiler arguments देखकर सही version चुनता है — program चलने से पहले ही decide.
int add(int,int) और double add(int,int) साथ में compile error है. Parameters number, type, या order में अलग होने चाहिए.Overriding example (runtime polymorphism)
class Animal {
void sound() { System.out.println("Some generic sound"); }
}
class Dog extends Animal {
@Override
void sound() { System.out.println("Bark!"); } // same signature
}
Animal a = new Dog(); // parent reference, child object
a.sound(); // कौन-सा version चलेगा?
Reference type Animal होने के बावजूद object का version चला — JVM runtime पर actual object देखकर decide करता है. यही runtime (dynamic) polymorphism है, Spring जैसे frameworks की रीढ़.
Comparison table
| Point | Overloading | Overriding |
|---|---|---|
| Parameters | अलग होने ज़रूरी | Identical होने ज़रूरी |
| Classes | Same class | Parent + child ज़रूरी |
| Resolve | Compile time (static) | Runtime (dynamic) |
| Return type | आज़ादी से अलग | Same या covariant |
| Access modifier | कोई भी | ज़्यादा restrictive नहीं हो सकता |
| static/final/private | Overload हो सकते हैं | Override नहीं हो सकते |
| Annotation | — | @Override (recommended) |
Overriding rules जो interviewers test करते हैं
- Same signature: name + parameters exactly match होने चाहिए.
- Access घट नहीं सकता: public parent method child में protected नहीं बन सकता.
- static methods: override नहीं होते — child में same-signature static method hiding है, जो object से नहीं, reference type से resolve होती है.
- final और private methods: override हो ही नहीं सकते.
- @Override annotation: optional है पर हमेशा लगाएं — compiler फिर signature typos पकड़ लेता है जो चुपचाप overload बना देते.
Frequently Asked Questions
What is the difference between overloading and overriding?
Overloading is same method name with different parameters in the same class, resolved at compile time; overriding is a child class redefining a parent method with the identical signature, resolved at runtime.
Can we override a static method in Java?
No. A same-signature static method in the child is method hiding, not overriding — it is resolved by reference type, not by the actual object.
Is changing only the return type overloading?
No, it is a compile error. Overloaded methods must differ in the number, type, or order of parameters.