Method Overloading vs Method Overriding
This is a classic and very high-frequency interview question. The confusion usually comes from compile-time vs runtime behavior and method signature rules.
High-Level Difference (One Line)
- Overloading → Same method name, different parameters (compile time)
- Overriding → Same method signature, different implementation (runtime)
Method Overloading
What Is Method Overloading?
Method overloading allows multiple methods with the same name in the same class (or subclass) with different parameter lists.
- ✔ Happens at compile time
- ✔ Also called static polymorphism
Rules of Method Overloading
- Method name must be same
-
Parameter list must be different
- Number of parameters OR
- Type of parameters OR
- Order of parameters
- Return type alone cannot differ
- Access modifier does not matter
- Can be in same class or subclass
Overloading Example
class Calculator {
int add(int a, int b) {
return a + b;
}
double add(double a, double b) {
return a + b;
}
int add(int a, int b, int c) {
return a + b + c;
}
}
- ✔ Valid overloading
- ✔ Method selection decided at compile time
Overloading and Type Promotion (Interview Trap)
void show(int a) {
System.out.println("int");
}
void show(long a) {
System.out.println("long");
}
show(10); // int
✔ Compiler chooses best match
Method Overriding
What Is Method Overriding?
Method overriding allows a subclass to provide a specific implementation of a method already defined in its parent class.
- ✔ Happens at runtime
- ✔ Also called dynamic polymorphism
Rules of Method Overriding
- Method name must be same
- Parameter list must be same
- Return type must be same or covariant
- Access modifier cannot be more restrictive
- Method must not be static, final, or private
- Requires inheritance
- Uses IS-A relationship
Overriding Example
class Animal {
void sound() {
System.out.println("Animal sound");
}
}
class Dog extends Animal {
@Override
void sound() {
System.out.println("Dog barks");
}
}
- ✔ Method resolved at runtime
- ✔ Based on object type
Runtime Polymorphism Example
Animal a = new Dog();
a.sound(); // Dog barks
- ✔ Reference type → Animal
- ✔ Object type → Dog
- ✔ Runtime decision
Overriding with Covariant Return Type
class A {
A get() {
return this;
}
}
class B extends A {
@Override
B get() {
return this;
}
}
- ✔ Allowed
- ✔ Java 5+
Static Methods: Overloading vs Overriding (Very Important)
Static Methods
- ✔ Can be overloaded
- ❌ Cannot be overridden (method hiding)
class Parent {
static void show() {
System.out.println("Parent");
}
}
class Child extends Parent {
static void show() {
System.out.println("Child");
}
}
Parent p = new Child();
p.show(); // Parent
✔ Compile-time binding
Overloading vs Overriding — Interview Comparison Table
| Aspect | Overloading | Overriding |
|---|---|---|
| Polymorphism | Compile-time | Runtime |
| Method name | Same | Same |
| Parameters | Must differ | Must be same |
| Return type | Can differ (not alone) | Same / covariant |
| Inheritance | ❌ Not required | ✔ Required |
| Access modifier | Any | Cannot reduce visibility |
| Static methods | ✔ Allowed | ❌ Not overridden |
| Binding time | Compile time | Runtime |
Common Interview Traps
- Changing return type only → ❌ not overloading
- Expecting static method overriding → ❌
- Confusing reference type vs object type
- Forgetting access-modifier rule
- Overriding private methods (not possible)
Interview-Ready Answers
Short Answer
Overloading is defining multiple methods with the same name but different parameters, while overriding is redefining a parent class method in a child class.
Detailed Answer
In Java, method overloading occurs when multiple methods share the same name but differ in parameters and is resolved at compile time. Method overriding occurs when a subclass provides its own implementation of a method defined in its superclass and is resolved at runtime based on the object type.
Key Takeaway
Overloading = compile-time decision
Overriding = runtime decision
Understanding this distinction is critical for polymorphism, inheritance, and interview success.