How does Java resolve a call to an overloaded method?

Method overloading in Java is resolved during compilation. The compiler examines the method name, number of arguments, parameter types, and their order to determine which overloaded method should be invoked.

Key Points: • Overloading is an example of compile-time polymorphism (static binding). • The compiler always tries to find the most specific method that matches the provided arguments. • Exact matches are preferred over type promotion, autoboxing, or varargs. • If multiple methods are equally suitable and the compiler cannot determine the best match, a compile-time ambiguity error occurs.

Example: If a class contains display(int) and display(double), calling display(10) invokes the int version because it is the most specific match. Calling display(10.5) invokes the double version.

Code Example:

class Demo {

    void show(int x) {
        System.out.println("int method");
    }

    void show(double x) {
        System.out.println("double method");
    }

    public static void main(String[] args) {
        Demo d = new Demo();

d.show(10); // int method d.show(10.5); // double method

    }
}

Interview Tip: A concise interview answer is: Java resolves overloaded methods at compile time by matching the method call with the most specific method signature based on the number, type, and order of arguments. If no suitable match exists or multiple methods are equally applicable, the compiler reports an error.