Is function overloading achieved through run-time polymorphism in Java. Also is operator overloading supported by Java?

Generally function overloading is achieved through run-time polymorphism in languages,but is the case in Java opposite? Because Oracle document says unless function is declared static ,it is loaded at run-time. So if a function is not loaded at compile time, then how can overloading occur?

Answers


In the case of function Overloading, it is static (compile-time) polymorphism because the compiler knows which method is to be called based on the number and type of parameters. For example:

public class FunctionOverloadingTest {
    public void display(String first) {
        System.out.println("I have one parameter");
    }

    public void display(String first, String second) {
        System.out.println("I have two parameters");
    }

    public static void main(String[] args) {
        FunctionOverloadingTest functionOverloadingTest= new FunctionOverloadingTest();
        functionOverloadingTest.display("one");
    }
}

In this case, compiler decides which display method is to be called based on the number of parameter passed in the function call.


No. Method overloading is compile time polymorphism.

In simple terms we can say that a class can have more than one methods with same name but with different number of arguments or different types of arguments or both.


Need Your Help

Recursive Function and Non-recursive Function Returning Different Results

java recursion while-loop

I'm creating two functions that are supposed to emulate and return the result of f(i) = 1/1 + 1/2 + 1/3 ... 1/i. One function is recursive, and I'm testing that the recursive function functions cor...

Stretching an image within a <li> with css IE8

css internet-explorer

I want to stretch an image used in an &lt;li&gt;, as the text within the &lt;li&gt; increases the size of the image size isn't big enough to increase along with the &lt;li&gt;. I do not want to rep...