The question is how the compiler can find out which method to call at compile time if overridden. You must understand this
List list = list.getAList(); list.add(whatever);
Now suppose the getAList() method can return any of several List implementations based on some criteria. So how can the compiler know which implementation is returning? and which add() method to call. As you can see, this can only be solved at runtime. While overloading is not the case, and at compile time everything is clear. I hope you understand this now.
[Edited]
Bringing the discussion into a comment on the actual answer.
It may not be known until complete. Understanding this, the creation of a particular class depends on the argument provided by the user. Now tell me how the compiler finds out which argument the user will pass, and apparently which class should instantiate. Or even simpler, answer this question, how will the compiler know if the stream will be passed to the if block or else ? Or why do you think we have checks and runtime exceptions? Take the divide-by-zero case; for example n/m , where m becomes 0 as a result of some calculation. In this case, it is obvious that the compiler will not be able to say what will be an ArithmeticException , because m not immediately known. Since all this information is not available at compile time, thus, the compiler likewise does not know which method will override.
Adele ansari
source share