Loading
Java Overloading-interview


Q1. What is method overloading in Java?

Method overloading means defining multiple methods in the same class with the same name but different parameter lists (number, type, or order of parameters). It is an example of compile-time polymorphism.

Q2. What are the rules of method overloading in Java?


  • Methods must differ in parameter type, number, or order.
  • Return type alone is not enough to overload.
  • Overloading can happen in the same class or in a subclass.
  • It works with constructors too (constructor overloading).


Q3. What is the difference between method overloading and method overriding?
  • Overloading Happens within the same class; based on different parameter lists; resolved at compile time.
  • Overriding Happens between superclass & subclass; same parameters; resolved at runtime.




Q4. Can return type alone differentiate overloaded methods? Write an example.


The compiler cannot distinguish methods only by return type.



class Demo {

    int test(int a) {

        return a;

    }

    //  Invalid: same parameters, only return type differs

    // double test(int a) {

    //     return a * 2.0;

    // }

}