java - Is overloading polymorphic? ?
给我你的怀抱
给我你的怀抱 2017-06-12 09:20:06
0
3
1052

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

给我你的怀抱
给我你的怀抱

reply all(3)
仅有的幸福

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() and PrintStream.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) and max(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:

  • To put it simply, it is one sentence: It is allowed to assign a pointer of a subclass type to a pointer of a parent class type. Polymorphism is implemented through virtual functions in both Object Pascal and C++.

  • Polymorphism: The same operation acts on different objects and can have different interpretations and produce different execution results. At runtime, you can call methods in a derived class through a pointer to the base class.

  • In C++, there are the following methods to achieve polymorphism: virtual functions, abstract classes, overrides, and templates (overloading has nothing to do with polymorphism).

  • Polymorphism allows methods to have the same name. Parameters or return values ​​can be passed in or returned from the parent type.

学霸

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.

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template