1 the concept

The same object has multiple forms

2 role

Unify different data types

Three characteristics

Parent class references to subclass objects (parent class XXX = new subclass ())

Above XXX can call the subclass to override the non-static method of the parent class, can call all the attributes and static methods of the parent class, but can not directly call the subclass unique method, must first be forced to subclass ((subclass) XXX). Subclass-specific methods (); In order to do that.

XXX = new subclass (); XXX = new subclass (); XXX = new subclass (); When XXX calls a method unique to the subclass, it needs to re-open a space only belonging to the subclass as ((subclass) XXX), so it needs to force the transfer. At this time, ((subclass) XXX) is equivalent to opening a subclass object space, which can be used to call a method unique to the subclass.Copy the code

Therefore, a parent class reference to the variable XXX that points to a subclass object cannot directly call subclass-specific methods

4 code:

The parent class Father. Java

public class Father {
    public String name = "fathername";
    public static String name2 = "fathername2";
​
    public void  run(a){
        System.out.println("f run");
    }
​
    public static void run2(a){
        System.out.println("f static run2");
    }
​
    public void fathermethod(a){
        System.out.println("f method"); }}Copy the code

Subclasses Son. Java

public class Son extends Father{
   public String name = "sonname";
    public static String name2 = "sonname2";
    
    public void  run(a){
        System.out.println("s run");
    }
​
    public static void run2(a){
        System.out.println("s static run2");
    }
​
    public void sonmethod(a){
        System.out.println("Subclass-specific methods"); }}Copy the code

The main program class application.java

public class Application {
    public static void main(String[] args) {
        Father f = new Son();
​
        f.run();                     // Call the run method overridden by the subclass.
        f.run2();                          // Run2 of the parent class (static method on the left)
        System.out.println(f.name2);       // Static property of the parent class name2 (properties on the left)
        System.out.println(f.name);        // Parent non-static property name (property on the left)
​
        ((Son) f).sonmethod();      // Call subclass-specific methods (see right for non-static methods)
                                    // Son XXX = ((Son)f); xxx.xonmethod();
        f.fathermethod();           // Call f method from the subclass, but not overridden.
        
        System.out.println("-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --");
        
        /* Father father = new Father(); (Son)father).run(); ((Son)father).fathermethod(); ((Son)father).sonmethod(); System.out.println(((Son) father).name); So a superclass reference to a variable that points to a superclass object cannot be forcefully cast to call something from a subclass. * /
        Son s = new Son();
        ((Father)s).run();      // Subclass overridden (non-static method see right)
        Father b = ((Father) s);
        System.out.println(b.name);  // Parent class (attributes see left)
        s.run();                       // The printable result is also overridden by Son
        s.run2();                     // Subclass run2 (static method see left)
        System.out.println(s.name2);// Subclass name2 (attributes see left)
        System.out.println(s.name);// Subclass overridden (attributes see left)}}Copy the code

5 Running Results

6 summarizes

A parent class reference to a variable pointing to a subclass object looks to the left when calling static methods and all properties (the parent class), and only looks to the right when calling non-static methods (a subclass).

So XXX in “parent XXX = new child ()” is simply a subclass object with subclass-specific methods removed.