Polymorphism is the ability of the same behavior to have multiple different manifestations or forms.
Polymorphism is the same interface, using different instances to perform different operations, as shown in the figure: (Recommended learning: java course)
#Polymorphism is the embodiment of multiple expressions of objects.
In reality, for example, when we press the F1 key:
如果当前在 Flash 界面下弹出的就是 AS 3 的帮助文档; 如果当前在 Word 下弹出的就是 Word 帮助; 在 Windows 下弹出的就是 Windows 帮助和支持。 同一个事件发生在不同的对象上会产生不同的结果。
Advantages of polymorphism
Eliminate the coupling relationship between types
Replaceability
Extensibility
Interface
Flexibility
Simplification
Three necessary conditions for the existence of polymorphism
Inheritance
Rewriting
The parent class reference points to the child class object
For example:
Parent p = new Child();
When calling a method using polymorphism, first check whether the method exists in the parent class. If not, a compilation error will occur; if so, call it again. Method with the same name of the subclass.
Benefits of polymorphism: It allows the program to be well expanded and can handle objects of all classes universally.
The above is the detailed content of What is java polymorphism. For more information, please follow other related articles on the PHP Chinese website!