Detailed explanation of polymorphism concept and implementation principle in Java

I What is polymorphism?

1. Definition of polymorphism

Allows different objects to respond to the same message. That is, the same message can adopt many different behavior modes according to different sending objects (sending a message is a function call)

2. The role of polymorphism

Eliminate coupling between types

3. Description of polymorphism

Modern network novels are rampant, we can use it to give an example

One day, you see that many novels on your mobile phone have been updated at the same time, such as the great master, the snow Eagle Lord and the legend of the Dragon King... Here we can describe it as follows:

Novel a = Master

Novel B = snow Eagle Lord

Novel C = legend of the Dragon King

What is shown here is polymorphism. The great master and the legend of the Dragon King are all subclasses of novels. We can reference different subclasses only through the parent class of novels. This is polymorphic C C. We will know the specific instance object pointed to by the reference variable only when running

Of course, this understanding is far from enough. To understand polymorphism, you must understand that it is "upward transformation"

In the above example, novel (XS) is the parent class, and dzz, xylz and LWCS are all its subclasses. Therefore, we define the following code

DZZ a=new DZZ();

You should not feel strange to this code, which is nothing more than instantiating a big master object. What about the following code?

XS a=new DZZ();

Here, we understand that an XS type A is defined to point to the dzz object instance. Because dzz inherits from XS, dzz can automatically transform upward to XS, so a can point to the instance object of dzz. This has a great advantage. In inheritance, we know that a subclass is an extension of the parent class, which can provide more powerful functions than the parent class. If we define a parent reference type pointing to a subclass, it can not only reference the commonness of the parent class, but also use the powerful functions of the child class

However, the upward transformation also has some shortcomings, that is, it will inevitably lead to the loss of some methods and attributes, so that we can not obtain them. Therefore, the application of the parent class type can call all the properties and methods defined in the parent class. It can't catch up with the methods and properties that only exist in the child class

Therefore, for polymorphism, we can summarize as follows: due to the upward transformation, the parent class reference pointing to the subclass can only access the methods and properties owned by the parent class. For the methods existing in the subclass but not in the parent class, the reference cannot be used, although it overloads the method. If the subclass overrides some methods in the parent class, these methods defined in the subclass must be used when calling these methods (dynamic connection and dynamic call)

For object-oriented, polymorphism is divided into compile time polymorphism and run-time polymorphism. Editing polymorphism is static, mainly refers to method overloading. It distinguishes different functions according to different parameter lists. After editing, it will become two different functions, so it is not polymorphic at run-time. The runtime polymorphism is dynamic, which is realized by dynamic binding, that is, what we call polymorphism.

II Implementation of polymorphism

1. Realization conditions

At the beginning, razor inheritance is preparing for the implementation of polymorphism. The subclass child inherits the parent class father. We can write a parent type reference to the subclass. This reference can handle both the parent class father object and the child class child object. When the same message is sent to the subclass or parent object, the object will perform different behaviors according to its own reference, which is polymorphism. That is, polymorphism is that the same message makes different classes respond differently

There are three necessary conditions for Java polymorphism: inheritance, rewriting and upward transformation

Inheritance: in polymorphism, there must be subclasses and parent classes with inheritance relationship

Override: a subclass redefines some methods in the parent class. When these methods are called, the subclass's methods will be called

Upward Transformation: in polymorphism, you need to assign the reference of the subclass to the parent object. Only in this way can the reference have the skills to call the methods of the parent class and the subclass

Only when the above three conditions are met can we use the same logic implementation code to process different objects in the same inheritance structure, so as to execute different behaviors

For Java, its polymorphic implementation mechanism follows a principle: when a superclass object references a subclass object with a reference variable, the type of the referenced object rather than the type of the reference variable determines whose member method to call, but the called method must be defined in the superclass, that is, the method covered by the subclass

2. Implementation form

inherit

In the above code, dzz and xylz inherit XS and override the drink(), tostring() method. The result of program operation is to call the method in the subclass and output the names of dzz and xylz, which is the expression of polymorphism. Different objects can perform the same behavior, but they all need to perform it through their own implementation methods, which will benefit from the upward transformation

We all know that all classes inherit from the superclass object, and the toString () method is also a method in object. When we write this:

The inheritance chain relationship of object, XS and dzz is: dzz ― > XS ― > object. So we can say this: when the method of the subclass overriding the parent class is called, only the method at the end of the object inheritance chain will be called. But note that if you write this:

Therefore, the polymorphism based on inheritance implementation can be summarized as follows: for the parent type that references a subclass, when processing the reference, it is applicable to all subclasses that inherit the parent class. Different subclass objects have different implementation of methods and different behaviors generated by executing the same action.

If the parent class is an abstract class, the child class must implement all the abstract methods in the parent class. In this way, all the child classes of the parent class must have a unified external interface, but their internal specific implementations can be different. In this way, we can use the unified interface provided by the top-level class to deal with the methods of this level.

The content of this article hopes to give some help to friends in need

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
分享
二维码
< <上一篇
下一篇>>