简体   繁体   中英

Is any method overloading be a compile time polymorphism?

I refer to some java tutorials and found java having two types of polymorphism.

  1. compile-time polymorphism(Static polymorphism)
  2. runtime polymorphism(Dynamic polymorphism)

some learning references are mentioned "If you overload a static method in Java, it is the example of compile-time polymorphism" . and some are mentioned "Method overloading is an example of compile-time polymorphism" .

what I want to know is if only static method overloading or every method overloading is the java compile-time polymorphism.?

Because when reading the first point I'm feeling why static method specially mentioned and why not instance methods and constructors are not mentioned.

Thanks

In Java, the choice, which of the overloads to invoke, is always made at compile-time. This applies to static methods, instance methods, and also constructors.

Note that the two statements are not contradicting. The first says, “If you overload a static method…”, which names a correct example, but doesn't preclude other examples. Likewise, the other statement “Method overloading is an example of compile time polymorphism” is broader, still correct, while not mentioning constructors. As long as these statements do not claim to have named all existing examples, they are correct.

Still, in case of instance methods the chosen overload can also be subject to runtime polymorphism when being overridden, in addition to the compile-time polymorphism. Having methods which are both, overloaded and overridden, can easily lead to errors, therefore should be used with care or avoided.

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