Java Polymorphism
Polymorphism is the ability of the same behavior to have multiple different forms or manifestations.
Polymorphism means using the same interface with different instances to perform different operations, as shown in the figure:

Polymorphism is the manifestation of an object's multiple forms.
In reality, for example, the action of pressing the F1 key:
The same event occurring on different objects produces different results.
Advantages of polymorphism
-
- Eliminate coupling between types
-
- Replaceability
-
- Extensibility
-
- Interface capability
-
- Flexibility
-
- Simplicity
Three necessary conditions for polymorphism to exist
- inheritance
- rewritten
- Parent class reference points to a child class object: Parent p = new Child();

class
Shape
{
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 the method; if not, a compile error occurs; if so, then call the subclass's method of the same name.
The benefit of polymorphism: it allows the program to have good extensibility and to process objects of all classes generically.
The following is a demonstration of a polymorphism example; see the comments for details:
Test.java File Code:
public
class
Test
{
public
static
void
main
(
String
[
]
args
)
{
show
(
new
Cat
(
)
)
;
//
Call the show method with a Cat object
show
(
new
Dog
(
)
)
;
//
Call the show method with a Dog object
Animal
a
=
new
Cat
(
)
;
//
Upcasting
a
.
eat
(
)
;
//
Calls Cat's eat
Cat
c
=
(
Cat
)
a
;
//
Downcasting
c
.
work
(
)
;
//
Calls Cat's work
}
public
static
void
show
(
Animal
a
)
{
a
.
eat
(
)
;
//
Type checking
if
(
a
instanceof
Cat
)
{
//
What a cat does
Cat
c
=
(
Cat
)
a
;
c
.
work
(
)
;
}
else
if
(
a
instanceof
Dog
)
{
//
What a 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 mice
"
)
;
}
}
class
Dog
extends
Animal
{
public
void
eat
(
)
{
System
.
out
.
println
(
"
Eat bones
"
)
;
}
public
void
work
(
)
{
System
.
out
.
println
(
"
Guard the house
"
)
;
}
}
Running the above program, the output is:
吃鱼
抓老鼠
吃骨头
看家
吃鱼
抓老鼠
Virtual Function
The existence of virtual functions is for polymorphism.
In fact, Java has no concept of virtual functions; its ordinary functions are equivalent to C++'s virtual functions, and dynamic binding is Java's default behavior. If you do not want a function in Java to have virtual function characteristics, you can add the final keyword to make it a non-virtual function.
rewritten
We will introduce how the behavior of overridden methods affects polymorphism when designing classes in Java.
We have discussed method overriding, which means a subclass can override a parent class method.
When a subclass object calls an overridden method, it is the subclass's method that is called, not the overridden method in the parent class.
To call the overridden method in the 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 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
;
}
}
Suppose the following class inherits from 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 is:
"
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 to:
"
getName
(
)
)
;
return
salary
/
52
;
}
}
Now let us carefully read the code below and try to give its output:
VirtualDemo.java file code:
/*
File name: 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 the Salary reference --
"
)
;
s
.
mailCheck
(
)
;
System
.
out
.
println
(
"
\n
Call mailCheck using the Employee reference --
"
)
;
e
.
mailCheck
(
)
;
}
}
The compilation and execution results of the above example are 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 s.mailCheck() is called, the compiler finds mailCheck() in the Salary class at compile time, and the JVM calls the Salary class's mailCheck() during execution.
-
e is an Employee reference, but the reference e ultimately runs the Salary class's mailCheck() method.
-
At compile time, the compiler uses the mailCheck() method in the Employee class to verify the statement, but at runtime, the Java Virtual Machine (JVM) calls the mailCheck() method in the Salary class.
The entire process above is called a virtual method invocation, and the method is called a virtual method.
All methods in Java can behave in this way, so overridden methods can be called at runtime, regardless of what data type the reference variable in the source code was at compile time.
Implementation of polymorphism
Method 1: Overriding:
This content has been explained in detail in the previous chapter and will not be repeated here; for details, see:
Java Override and Overload
.
Method 2: Interface
-
1. The most representative interface in life is the socket; for example, a three-pin plug can be inserted into a three-hole socket, because each country has its own defined interface rules, which may not work abroad because foreign countries define their own interface types.
-
2. Interfaces in Java are similar to interfaces in life; they are collections of method signatures without method implementations. For details, see Java interface the content of this chapter.
Method 3: Abstract classes and abstract methods
For details, see Java abstract class section.