Difference Between Method Overloading and Method Overriding in Java — Java Technical Interview Questions & Answers
Difference Between Method Overloading and Method Overriding in Java
Difference Between Method Overloading and Method Overriding in Java
- Overloading: same name, different parameters, same class, compile time. Overriding: child rewrites parent method with the same signature, runtime. Return type alone cannot overload.
- Show overloading: add(int,int) and add(double,double). Show overriding: Animal a = new Dog(); a.sound(); → Dog’s version. Use @Override.
- Don’t mix the two words. Two timings, two examples.
- Let's take this on the board with one tiny Main class — no extra files. Difference Between Method Overloading and Method Overriding 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 Difference Between Method Overloading and Method Overriding.
Technical Interview Tip: Two timings + two examples. Return type alone cannot overload.
Code Examples for Difference Between Method Overloading and Method Overriding 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));
}
}Difference Between Method Overloading and Method Overriding in Java — compiler picks the matching add based on argument types.
How to Explain Difference Between Method Overloading and Method Overriding in Java to an Interviewer
When is overloading decided?
Compile time
When is overriding decided?
Runtime