Elaboration: Method overloading is a static/compile-time binding but not polymorphism. Is it correct to correlate static binding with polymorphism?

早过忘川 提交于 2019-12-02 04:55:27

In the Java world, polymorphism means polymorphism between classes. I.e. refering possibly multiple child classes with their common parent. In Java, there is no polymorphism between methods.

void add(int a, int b) and void add(int a, int b, int c) are entirely different methods in the Java syntax. It shouldn't be so - for example, in C++, you can cast them to each other -, but it is so in Java.

The key concept to understand here is the method signature. The method signature defines in a language, what identifies the induvidual methods. (For example, beside a void add(int a, int b);, you simply can't declare an int add(int a, int b); method - the return value is not a part of the method signature in Java, thus the compiler would interpret it as a method re-definition.)

People says

  1. overriding is run time polymorphism and
  2. overloading is compile time polymorphism.

Both are wrong.
Only Overriding is not polymorphism. But overriding helps to achieve ploymorphism. If there is no upcasting you can't achieve ploymorphism.

Overloading is a concept where method-name along with argument signature are used to bind method call to method body and it can be predicted during compile time only. But this is nowhere related to polymorphism. There is NO behavioral change can be found in case of method overloading. Either you compile today or compile after one year behavioral change can only found if you change calling method signature i.e, only if you modify code say add(3,4); to add(3,4,5); and hence method overloading is not polymorphism.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!