This problem is really a headache, no matter what you say
View 1: The three elements of polymorphism, 1,2,3
So overloading is not polymorphism
View 2:
Mentioned in the book java virtual machine
Some of the most basic manifestations of polymorphism characteristics, such as "overloading" and "rewriting" Section 8.1
This means that overloading and rewriting are both manifestations of polymorphism
How do you understand
Each book is translated differently, it is best to specify the English term, whether it is Override or Overload.
Override is an embodiment of polymorphism (e.g.
@Override public String toString() {...}
), but Overload is not (e.g.PrintStream.println()
andPrintStream.println(String s)
).In fact, there are three English equivalents of the Chinese "overload": override, overload and overwrite. Later, the last one was gradually translated into "rewrite" or "rewrite".
Override refers to the practice of redefining functions of the parent class with the same signature. In this case, the parent class function and the child class function are independent entities. But when calling, the compiler (or interpreter) can determine which implementation to call based on the actual type of the object.
overload refers to multiple implementations of the same function name that cannot be signed. In this case, the same function name can often be called with different types of parameters to produce different results. Typical examples are
max(int, int)
andmax(double, double)
etc.overwrite is usually the rewriting of a function with the same signature of the parent class by a subclass. The signatures of the two functions are the same, but they have nothing to do with each other. In this case, the parent class type refers to the subclass object, and the calling method is the parent class method called (note the difference with override)
About the difference between overwrite and override, as shown below
Now let’s talk about polymorphism. Let’s first look at the definition of polymorphism:
Polymorphism refers to the same entity having multiple forms at the same time. (Bing Music)
Polymorphism literally means "multiple states". In object-oriented languages, multiple different implementations of an interface are called polymorphism. (Baidu Encyclopedia)
From the definition, "overloading" has nothing to do with polymorphism, but overloading is a form of polymorphism. Others include interfaces, abstractions, virtual functions, templates, generics..., and then from Baidu A few quotes from the encyclopedia:
There may be translation errors. Polymorphism means that objects show different characteristics in different contexts. For example, a subclass Child inherits the parent class parent. There is a login method in parent, and a new method is added to the Child class, save. . Also directly use Child child1=new Child(), the child1 object can call the save and login methods, use Parent child2 = new Child(), the child2 object can only use the login method. So it depends on the context.