Java polymorphism (power node Java College)

What is polymorphism

1. Three characteristics of object-oriented: encapsulation, inheritance and polymorphism. From a certain point of view, encapsulation and inheritance are almost always prepared for polymorphism. This is our last concept and the most important knowledge point.

2. Definition of polymorphism: it means that different objects are allowed 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)

3. The technology to realize polymorphism is called dynamic binding, which means to judge the actual type of the referenced object during execution and call its corresponding methods according to its actual type.

4. The role of polymorphism: eliminate the coupling relationship between types.

5. In reality, there are numerous examples of polymorphism. For example, if you press F1, the help document of as 3 will pop up in the flash interface; If word help pops up under word; Windows help and support will pop up under windows. When the same event occurs on different objects, it will produce different results.

The following are the three necessary conditions for the existence of polymorphism. It is best to remember them on the basis of understanding.

Three necessary conditions for the existence of polymorphism

1、 There should be inheritance;

2、 There should be rewriting;

3、 A parent class reference points to a child class object.

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 characteristics 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 is realized by superclass providing a common interface to subclasses through method signature, which is improved or overwritten by subclasses, as shown in Figure 8.3. 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 applications and improves use efficiency.

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

The implementation of polymorphism in Java: interface implementation, method rewriting by inheriting the parent class, and method overloading in the same class.

A small topic:

(1) Related classes

(2) Question: what are the following output results?

(3) Answer

① A and A ② A and A ③ A and D ④ B and A ⑤ B and A ⑥ A and D ⑦ B and B ⑧ B and B ⑨ A and D

The above is the Java polymorphism (power node Java college sorting) introduced by Xiaobian. I hope it will be helpful to you. If you have any questions, please leave me a message. Xiaobian will reply to you in time. Thank you very much for your support for the programming tips website!

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