Java - Polymorphism

1. Definition of polymorphism in Java

  1. The premise of polymorphism must have a child-parent class relationship , or a class-implementation interface relationship

  2. The final manifestation of polymorphism is: the parent class reference points to the subclass object ; when the parent class reference calls a method, if the subclass rewrites the method, the rewritten method of the subclass will be called. If the method is not rewritten, The method in the parent class will be called ( member method, when compiling, the parent class will be retrieved, if the parent class has this method, the compilation is successful! At runtime, the rewritten method of the subclass will be run first ). example:   

package com.xiao.variable;

/**
 * @Author laughs
 * @Date 19:55 2018/04/22
 */
public class Father {

    public void test_01(){
        System.out.println("Father");
    }
}
package com.xiao.variable;

/**
 * @Author laughs
 * @Date 19:56 2018/04/22
 */
public class Son extends Father{
    //Override the method of the parent class
    public void test_01() {
        System.out.println("Son");
    }
}
package com.xiao.variable;

/**
 * @Author laughs
 * @Date 19:58 2018/04/22
 */
public class Test {
    public static void main(String[] args){
        //The parent class reference points to the child class object
        Father f = new Son();
        //Use the parent class reference to call the rewritten method of the subclass, the following print result is Son
        f.test_01();
    }

}

Comment out the overridden method of the subclass and call it again, as follows:

package com.xiao.variable;

/**
 * @Author laughs
 * @Date 19:56 2018/04/22
 */
public class Son extends Father{

    //Override the method of the parent class
    /*public void test_01() {
        System.out.println("Son");
    }*/
}
package com.xiao.variable;

/**
 * @Author laughs
 * @Date 19:58 2018/04/22
 */
public class Test {
    public static void main(String[] args){
        //The parent class reference points to the child class object
        Father f = new Son();
        //Because the subclass does not override this method, the method of the parent class is called, and the print result is Father
        f.test_01();

    }

}

2. Characteristics of member variables

  Member variables, the parent class is retrieved when compiling, if there is a parent class, the compilation is successful; when running, the value of the member variable in the parent class will also be run, for example:

package com.xiao.variable;

/**
 * @Author laughs
 * @Date 19:55 2018/04/22
 */
public class Father {

    // member variable a in the parent class
    int a = 100;

    public void test_01(){
        System.out.println("Father");
    }
}
package com.xiao.variable;

/**
 * @Author laughs
 * @Date 19:56 2018/04/22
 */
public class Son extends Father{

    // member variable a in the subclass
    int a = 1;

    //Override the method of the parent class
    public void test_01() {
        System.out.println("Son");
    }
}
package com.xiao.variable;

/**
 * @Author laughs
 * @Date 19:58 2018/04/22
 */
public class Test {
    public static void main(String[] args){
        //The parent class reference points to the child class object
        Father f = new Son();

       //Use the parent class reference to call the member variable, and the print result is 100
        System.out.println(f.a);

    }

}


Three. instanceof keyword

 instanceof is a comparison operator that determines whether a reference type belongs to an object of a certain type (with inheritance or implementation relationship) , for example:

package com.xiao.variable;

/**
 * @Author laughs
 * @Date 19:58 2018/04/22
 */
public class Test {
    public static void main(String[] args){
        //The parent class reference points to the child class object
        Father f = new Son();
        
        //Determine whether f is an object of type Father, returns a boolean value, the following print result is true
        boolean b = f instanceof Father;
        System.out.println(b);

    }

}


4. Up and down transitions in polymorphism

  In Java, the parent class reference points to the subclass object, and automatic type promotion has been done to promote the subclass type to the parent class, which is upcasting. But sometimes, we need to downcast when we want to use a method unique to the subclass . example:

package com.xiao.variable;

/**
 * @Author laughs
 * @Date 19:56 2018/04/22
 */
public class Son extends Father{

    //Override the method of the parent class
    public void test_01() {
        System.out.println("Son");
    }

    //methods specific to subclasses
    public void son(){
        System.out.println("I am a subclass");
    }
}
package com.xiao.variable;

/**
 * @Author laughs
 * @Date 19:58 2018/04/22
 */
public class Test {
    public static void main(String[] args){

       //The parent class reference points to the child class object
        Father f = new Son();

        // Downcast, to force conversion
        Son s = (Son) f;

        //The subclass object calls the subclass-specific method, and the print result is "I am a subclass"
        s.son();


    }

}



 



Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326560408&siteId=291194637