Is Method Overloading a Type of Polymorphism? [dup

2019-05-29 20:16发布

This question already has an answer here:

I was studying about static and dynamic polymorphism and got these links:

http://guruzon.com/1/oop-concepts/polymorphism/what-is-polymorphism-example-tutorial-uml-diagram-notation

http://www.coderanch.com/t/379004/java/java/static-polymorphism-dynamic-polymorphism

http://javarevisited.blogspot.in/2011/08/what-is-polymorphism-in-java-example.html

In all these links , It has been said that Overloading is an example of polymorphism , then I came across two more places where it has been said that overloading has nothing to do with polymorphism, the links are:

Is Method Overloading considered polymorphism?

Head First Java 2nd Edition Chapter 7 (Inheritence and Polymorphism) Last topic: Overloading a methodScreenshot of headfirst page.

Now I am not able to figure out whether it is a kind of polymorphism or not and if it is not the is there anything called static polymorphism in OOPs ?

1条回答
做个烂人
2楼-- · 2019-05-29 20:32

Sometimes, method overloading is indeed referred to as an example of "static polymorphism". This designation is somewhat arbitrary indeed, but it is possible to make a weak defense in its favor.

The concept of static polymorphism does exist, and it is not limited to OOP. Polymorphism (an ability to present the same behavior in different forms) can be static when polymorphic behavior is achieved based on compile-time type information. The most common example of such polymorphism is templates of C++: all calls are resolved statically, yet the behavior of a template is altered based on the static type of its type parameters.

Overloading could be thought of as a primitive form of choosing the behavior based on the static type of an object, too. However, this view of the overloading does not help understanding of the concept, and also makes it harder for the newcomers to understand the "real" (i.e. dynamic) polymorphism. That is why the term "static polymorphism" is not common in books on Java.

查看更多
登录 后发表回答