Polymorphism is the ability of the same behavior to have multiple different manifestations or forms.
Polymorphism is the same interface, using different instances and performing different operations, as shown:

Polymorphism is a manifestation of many forms of object expression.
In reality, let's say we press the F1 key:
- If you are currently popping up under the Flash interface, it is the AS 3 help file.
- If Word pops up under Word, it is Word Help.
- What pops up under Windows is Windows Help and Support.
The same event occurs on different objects and produces different results.
The advantages of polymorphism
- 1. Eliminating the coupling between types
- 2. Replaceability
- 3. Expandability
- 4. Interface
- 5. Flexibility
- 6. Simplification
Three Necessary Conditions for the Existence of Polymorphism
- inherit
- Rewrite
- Parent reference to subclass object
such as:
Parent p = new Child ();
When using a polymorphic method to call a method, first check if there is a method in the parent class. If not, then the compile error occurs; if so, call the method with the same name of the subclass.
The benefits of polymorphism: You can make programs well-extended, and you can perform generic processing on objects of all classes.
The following is a demonstration of a polymorphic instance. For detailed instructions, see the notes:
Test.java file code:
public class Test {
public static void main(String[] args) {
show ( new Cat ( ) ) ; // Call show method with Cat object
show ( new Dog ( ) ) ; // Call the show method as a Dog object
Animal a = new Cat ( ) ; // Upward transition
a . eat ( ) ; // call Cat's eat
Cat c = ( Cat ) a ; // downcast
c . work ( ) ; // Call Cat's work
}
public static void show ( Animal a ) {
a . eat ( ) ;
// type judgment
if ( a instanceof Cat ) { // what cats do
Cat c = ( Cat ) a ;
c . work ( ) ;
} else if ( a instanceof Dog ) { // Things the dog does
Dog c = ( Dog ) a ;
c . work ( ) ;
}
}
}
abstract class Animal {
abstract void eat ( ) ; }
class Cat extends Animal {
public void eat ( ) {
System . out . println ( " Eat fish " ) ; }
public void work ( ) {
System . out . println ( " catch mouse " ) ; }
}
class Dog extends Animal {
public void eat ( ) {
System . out . println ( " eat bones " ) ; }
public void work ( ) {
System . out . println ( " Watching home " ) ; }
}
}
abstract class Animal {
abstract void eat ( ) ; }
class Cat extends Animal {
public void eat ( ) {
System . out . println ( " Eat fish " ) ; }
public void work ( ) {
System . out . println ( " catch mouse " ) ; }
}
class Dog extends Animal {
public void eat ( ) {
System . out . println ( " eat bones " ) ; }
public void work ( ) {
System . out . println ( " Watching home " ) ; }
}
After executing the above program, the output is:
Eat fish catch mouse eat bones Watching home Eat fish catch mouse
Virtual method
We will introduce how the behavior of the overridden method affects the polymorphism when designing a class in Java.
We have already discussed method rewriting, that is, subclasses can override methods of the parent class.
When a subclass object invokes an overridden method, the subclass method is called instead of the overridden method in the parent class.
To call a method that is being overridden in a parent class, you must use the keyword super.
Employee.java file code:
/* File name: Employee.java */
public class Employee {
private String name; private String address; private int number; public Employee(String name, String address, int number) { System.out.println("Employee constructor"); this.name = name; this.address = address; this.number = number; } public void mailCheck() { System.out.println("Mail the check to: " + this.name + " " + this.address); } public String toString() { return name + " " + address + " " + number; } public String getName() { return name; } public String getAddress() { return address; } public void setAddress(String newAddress) { address = newAddress; } public int getNumber() { return number; } }
public class Employee {
private String name; private String address; private int number; public Employee(String name, String address, int number) { System.out.println("Employee constructor"); this.name = name; this.address = address; this.number = number; } public void mailCheck() { System.out.println("Mail the check to: " + this.name + " " + this.address); } public String toString() { return name + " " + address + " " + number; } public String getName() { return name; } public String getAddress() { return address; } public void setAddress(String newAddress) { address = newAddress; } public int getNumber() { return number; } }
Assume that the following class extends the Employee class:
Salary.java file code:
/* File name: Salary.java */
public class Salary extends Employee
{
private double salary; // Annual salary
public Salary(String name, String address, int number, double salary) {
super(name, address, number);
setSalary(salary);
}
public void mailCheck() {
System.out.println(" The mailCheck method of the Salary class ");
System.out.println(" Mail check to:" + getName()
+ " ,Salary:" + salary);
}
public double getSalary() {
return salary;
}
public void setSalary(double newSalary) {
if(newSalary >= 0.0) {
salary = newSalary;
}
}
public double computePay() {
System.out.println("Calculate salary, pay:" + getName());
return salary/52;
}
}
Now we read the following code carefully and try to give its output:
VirtualDemo.java file code:
/* Filename: VirtualDemo.java */
public class VirtualDemo { public static void main(String [] args) {
Salary s = new Salary ( " Employee A " , " Beijing " , 3 , 3600.00 ) ; Employee e = new Salary ( " Employee B " , " Shanghai " , 2 , 2400.00 ) ; System . out . println ( " Call mailCheck using a reference to Salary -- " ) ; s . mailCheck ( ) ; System . out . println ( " \nCallCheck-- " using a reference to Employee" ) ; e . mailCheck ( ) ; }
}
public class VirtualDemo { public static void main(String [] args) {
Salary s = new Salary ( " Employee A " , " Beijing " , 3 , 3600.00 ) ; Employee e = new Salary ( " Employee B " , " Shanghai " , 2 , 2400.00 ) ; System . out . println ( " Call mailCheck using a reference to Salary -- " ) ; s . mailCheck ( ) ; System . out . println ( " \nCallCheck-- " using a reference to Employee" ) ; e . mailCheck ( ) ; }
}
The above example compiled and run results are as follows:
Employee constructor Employee constructor Call mailCheck using a reference to Salary -- The mailCheck method of the Salary class Mail check to: Employee A ,Salary:3600.0 CallCheck-- using a reference to Employee The mailCheck method of the Salary class Mail check to: Employee B ,Salary:2400.0
Example analysis
- In the instance, two Salary objects are instantiated: one uses the Salary reference s, and the other uses the Employee reference e.
- When s.mailCheck() is called, the compiler finds mailCheck() in the Salary class at compile time, and the JVM invokes mailCheck() of the Salary class during execution.
- Because e is a reference to Employee, when calling e's mailCheck() method, the compiler looks for the mailCheck() method in the Employee class.
- At compile time, the compiler validates the statement using the mailCheck() method in the Employee class, but at run time, the Java Virtual Machine (JVM) calls the mailCheck() method in the Salary class.
The entire process above is called a virtual method call, which is called a virtual method.
All methods in Java can behave in this way, so rewriting methods can be called at runtime, regardless of the data type of the referenced variable in the source code at compile time.
Polymorphic implementation
Method One: Rewrite:
This content has been covered in detail in the previous chapter and will not be elaborated. For more details, visit: Java Override and Overload .Method 2: Interface
- 1. The most representative interface in life is the socket. For example, a three-connector plug can be connected to a three-hole socket. This is because each country has its own interface rules and may not be able to go abroad. That's because of the types of interfaces that have been defined abroad.
- 2. The interface in java is similar to the interface in life, which is a collection of some method features, but there is no implementation of the method. See the java interface section for details.
Method 3: Abstract Classes and Abstract Methods
See the Java abstract classes section for details.
Comments
Post a Comment