Java Polymorphism
Polymorphism is the ability of the same behavior to have multiple different forms or manifestations.
Polymorphism is the same interface, using different instances to perform different operations, as shown in the figure:
Polymorphism is the manifestation of objects in multiple forms.
In real life, for example, the action of pressing the F1 key:
- If you are currently in the Flash interface, the AS 3 help documentation pops up;
- If you are currently in Word, the Word help pops up;
- Under Windows, the Windows help and support pops up.
The same event occurring on different objects produces different results.
Advantages of Polymorphism
- 1. Eliminate coupling relationships between types
- 2. Replaceability
- 3. Extensibility
- 4. Interface capability
- 5. Flexibility
- 6. Simplification
Three Necessary Conditions for Polymorphism
- Inheritance
- Overriding
- A parent class reference points to a subclass object:Parent p = new Child();

void draw() {}
}
class Circle extends Shape {
void draw() {
System.out.println("Circle.draw()");
}
}
class Square extends Shape {
void draw() {
System.out.println("Square.draw()");
}
}
class Triangle extends Shape {
void draw() {
System.out.println("Triangle.draw()");
}
}
When calling a method using polymorphism, first check whether the parent class has that method. If not, a compilation error occurs; if it does, call the subclass's method with the same name.
Benefit of polymorphism: it can give programs good extensibility and allow all class objects to be handled generically.
The following is a demonstration of a polymorphic example. For a detailed explanation, please see the comments:
Test.java file code:
Execute the above program, the output result is:
eat fish catch mice eat bones guard the house eat fish catch mice
Virtual Functions
Virtual functions exist for polymorphism.
Java actually does not have the concept of virtual functions. Its ordinary functions are equivalent to C++ virtual functions, and dynamic binding is the default behavior in Java. If you do not want a function to have virtual function characteristics in Java, you can add the final keyword to make it a non-virtual function.
Overriding
We will introduce how the behavior of overridden methods affects polymorphism when designing classes in Java.
We have already discussed method overriding, that is, subclasses can override methods of the parent class.
When a subclass object calls an overridden method, it calls the subclass's method, not the overridden method in the parent class.
To call the overridden method in the parent class, you must use the keywordsuper。
Employee.java file code:
Assume the following class inherits the Employee class:
Salary.java file code:
Now let's read the following code carefully and try to give its output result:
VirtualDemo.java file code:
The compilation and running result of the above example is as follows:
Employee 构造函数 Employee 构造函数 使用 Salary 的引用调用 mailCheck -- Salary 类的 mailCheck 方法 邮寄支票给:员工 A ,工资为:3600.0 使用 Employee 的引用调用 mailCheck-- Salary 类的 mailCheck 方法 邮寄支票给:员工 B ,工资为:2400.0
Example Analysis
-
In the example, two Salary objects are instantiated: one using the Salary reference s, and the other using the Employee reference e.
-
When calling s.mailCheck(), the compiler finds mailCheck() in the Salary class at compile time, and during execution the JVM calls the mailCheck() of the Salary class.
-
e is an Employee reference, but the reference e ultimately runs the mailCheck() method of the Salary class.
-
At compile time, the compiler uses the mailCheck() method in the Employee class to validate the statement, but at runtime, the Java Virtual Machine (JVM) calls the mailCheck() method in the Salary class.
The whole process above is called virtual method invocation, and the method is called a virtual method.
All methods in Java can behave in this way. Therefore, overridden methods can be called at runtime regardless of what data type the reference variable is in the source code at compile time.
Ways to Implement Polymorphism
Method 1: Overriding:
This content has already been discussed in detail in the previous chapter, so no further elaboration is given. For details, please visit:Java Override and Overload。Method 2: Interfaces
1. The most representative interface in life is a socket. For example, a plug with three pins can be inserted into a three-hole socket, because each country has its own interface rules. It may not work abroad because foreign countries define their own interface types.
2. An interface in Java is similar to an interface in life; it is a collection of method signatures, but without method implementations. For details, seeJava interfacesin this chapter.
Method 3: Abstract Classes and Abstract Methods
For details, seeJava Abstract Classeschapter.
Other Extensions