-4

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?

Amisha Bansal
  • 11
  • 1
  • 4

2 Answers2

0

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.

Prateek Kapoor
  • 947
  • 9
  • 18
-1

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.

Raman Shrivastava
  • 2,923
  • 15
  • 26
  • Are you sure about that? http://stackoverflow.com/questions/824763/is-polymorphism-another-term-for-overloading – bhspencer Jul 01 '15 at 15:14
  • 1
    Is it just me or is there something really wrong with the terminology of *compile-time polymorphism* – Chetan Kinger Jul 01 '15 at 15:16
  • According to [this question](http://stackoverflow.com/questions/20783266/what-is-the-difference-between-dynamic-and-static-polymorphism-in-java), method overloading can be considered static (compile-time) polymorphism – AniDev Jul 01 '15 at 15:19
  • My question is when function is not loaded at compile time , so how can its overloading occur? – Amisha Bansal Jul 01 '15 at 15:21
  • What do you mean "when function is not loaded at compile time" all functions are loaded at compile time, thats how they are compiled – bhspencer Jul 01 '15 at 15:22
  • Can you post where in the Oracle Documentation it says that? – Jake Jul 01 '15 at 15:32
  • @AniDev I don't know about the answers but the [Oracle](https://docs.oracle.com/javase/tutorial/java/IandI/polymorphism.html) documentation on `polymorphism` only talks about *overriding* methods. There is no mention of overloading in the context of `polymorphism` in the documentation. – Chetan Kinger Jul 01 '15 at 15:40
  • @ChetanKinger That's fair enough; I just mentioned that other question because it seemed to worth pointing out that two SO questions disagreed about whether overloading is polymorphism or not. It seems to be more of a general programming concept than anything specific to Java. – AniDev Jul 01 '15 at 15:54
  • Yes there is no mention of function overloading as polymorphism in documentation. The books say that and the previous answers on other links mention it to be compile time polymorphism. I read somewhere that unless a method is declared static,it gets memory in method area at run time, so my question was how come overloading is said to be a compile time polymorphism. While some answers also say it is not not even polymorphism. – Amisha Bansal Jul 01 '15 at 15:55
  • @AmishaBansal There is no link between your previous comment and your question. – Chetan Kinger Jul 01 '15 at 16:03