Java polymorphism is introduced from shallow to deep

What is polymorphism?

There are two types of polymorphism:

(1) Compile time polymorphism (design time polymorphism): method overloading.

(2) Runtime polymorphism: the Java runtime system determines which method to call according to the type of the instance calling the method, which is called runtime polymorphism. (we usually talk a lot about runtime polymorphism, so polymorphism mainly refers to runtime polymorphism)

Three necessary conditions for the existence of runtime polymorphism: first, inheritance (including the implementation of the interface); second, rewriting; third, the parent class reference points to the child class object.

--------------------------------------------------------------------------------

Detailed explanation:

Explanation of runtime polymorphism: A. runtime polymorphism refers to the specific type pointed to by the reference variable defined in the program and B. the method call issued through the reference variable is not determined during programming, but only during program operation, that is, which class instance object a reference variable will point to, The method call issued by the reference variable can only be determined during the running of the program

1. The specific type pointed to by the reference variable defined in the program sequence is uncertain (that is, which class instance object a reference variable will point to).

example:

Drive method in driver class (vehicle class) {}

•oneDriver. drive( new car() ) •oneDriver. Drive (New bus()) where the vehicle variable cannot determine which subclass instance to use.

1. The method call issued through the reference variable is uncertain during programming (the method call issued by the reference variable is the method implemented in which class).

Example: chef, gardener, barber cut method call persion. cut().

--------------------------------------------------------------------------------

Benefits of polymorphism:

1. Substitutability. Polymorphic pairs are replaceable for existing code. For example, polymorphic pairs work for the circle class, as do any other circular geometry, such as a ring.

2. Extensibility. Polymorphism is extensible to code. Adding new subclasses does not affect the polymorphism, inheritance, and operation of other features of existing classes. In fact, it is easier for new subclasses to obtain polymorphism functions. For example, it is easy to add the polymorphism of sphere class on the basis of realizing the polymorphism of cone, semi cone and hemisphere.

3. Interface capability. Polymorphism refers to that a superclass provides a common interface to a subclass through method signature, and the subclass improves or overwrites it, as shown in Figure 8.3. The superclass shape in the figure specifies two interface methods that implement polymorphism, computerarea() and computevolume(). Subclasses, such as circle and sphere, improve or cover these two interface methods in order to achieve polymorphism.

4. Flexibility. It embodies flexible and diverse operations in the application and improves the use efficiency.

5. Simplicity. Polymorphism simplifies the code writing and modification process of application software, especially when dealing with the operation and operation of a large number of objects.

Practical application:

Combined with the use of the configuration file, contact the spring framework, use reflection to dynamically call classes, and directly add new classes and modify the configuration file without modifying the source code. You can extend the program without restarting the server.

--------------------------------------------------------------------------------

Summary:

Use the reference of the parent type to point to the object of the child class. The variables of the methods and variables defined in the master class called by the reference cannot be overridden (overwritten); if a method in the parent class is overridden in the child class, the method in the child class will be called when calling this method;

Note that in special cases, if the parameter list of the method called by the parent class reference is not defined, it will call the parent class of the parent class to find it. If it is not found, it will force the upward type conversion of the parameter type in the parameter list. The specific priorities are as follows:

this. show(O)、super. show(O)、this. show((super)O)、super. show((super)O)。

--------------------------------------------------------------------------------

Classic written test questions (mixed overloading and rewriting):

(1) Related classes

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>