Method Overloading in Java — Java Technical Interview Questions & Answers
Method Overloading in Java
Method Overloading in Java
- Overloading = same method name in the same class with different parameter lists. Change the number or types: add(int, int) vs add(double, double). The compiler picks the match from the arguments you pass.
- Return type alone does not create a valid overload. That is the trap. Overloading is handy for convenience APIs — print with many input shapes.
- Interview line: same name, different params, compile-time. Return type alone is not enough.
- Let's take this on the board with one tiny Main class — no extra files. Method Overloading in Java — compiler picks the matching add based on argument types. Start from main, go line by line, and stop at each print. That output is the proof for Method Overloading.
Technical Interview Tip: Stress: return type alone cannot overload.
Code Examples for Method Overloading in Java
Example
// Method overloading
public class Main {
static int add(int a, int b) { return a + b; }
static double add(double a, double b) { return a + b; }
public static void main(String[] args) {
System.out.println(add(2, 3));
System.out.println(add(2.5, 3.5));
}
}Method Overloading in Java — compiler picks the matching add based on argument types.
How to Explain Method Overloading in Java to an Interviewer
What is overloading?
Same name Different parameters Same class
When is overloading decided?
Compile time
Can return type alone overload?
No