简体   繁体   中英

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM