Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Java Polymorphism

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:

  • If you are currently in the Flash interface, the AS 3 help document pops up;
  • If you are currently in Word, the Word help pops up;
  • In Windows, the Windows Help and Support pops up.
  • The same event occurring on different objects produces different results.

    Advantages of polymorphism

    Three necessary conditions for polymorphism to exist

    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

    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

    Method 3: Abstract classes and abstract methods

    For details, see Java abstract class section.