Can method overloading be determined at runtime?

Method overloading is resolved entirely at compile time, not at runtime. The Java compiler decides which overloaded method to invoke by examining the method name, number of parameters, parameter types, and the arguments provided during the method call. Once the code is compiled, the selected method is fixed and no further decision is made at runtime.

Key Points: • Method overloading is an example of compile-time (static) polymorphism. • The compiler selects the most specific matching method based on the method signature and argument types. • Runtime polymorphism applies to method overriding, where the actual object type determines which method is executed.

Example: If a class contains display(int) and display(String), the compiler determines which method to call based on the argument passed. This decision is made before the program runs.

Code Example:

class Demo {

    void display(int number) {
        System.out.println("Integer method");
    }

    void display(String text) {
        System.out.println("String method");
    }
}

public class Main {

    public static void main(String[] args) {

        Demo demo = new Demo();

demo.display(10); // Calls display(int) demo.display("Java"); // Calls display(String)

    }
}

Interview Tip: A concise interview answer is: No, method overloading is not determined at runtime. It is resolved at compile time based on the method signature and argument types. This is known as compile-time polymorphism, whereas method overriding is resolved at runtime through dynamic method dispatch.