Understanding of upcasting and downcasting in java

First we declare an Animal class, which is the parent class. Declare a Dog class, which is a subclass. In the Dog class, the main method is tested.
package PrimaryTest;

public class Animal {
    float weight ;
    int height ;
    public void run(){
        System.out.println("I am an animal,i can run");
    }

    public void cry(){
        System.out.println("i am an animal ,i can cry");
    }
}
The following is the transition test:
package PrimaryTest;

public class Dog extends Animal{
    public void run(){ //Subclass overrides the run method
        System.out.println("i am a god , i can run , different from others ");
    }

    public void bark(){ //Subclass-specific bark method
        System.out.println("i am dog ,i can bark , wang wang wang !");
    }

    public static void main(String[] args) {
        //The reference of the parent class of the upcast points to the subclass object. The subclass object is treated as the parent class object, and the subclass will lose its own unique methods.
        Animal animal = new Dog();
        //animal.bark is already wrong, this method can no longer be used. At this time, the new dog dog object has been upcast into an animal object, losing its bark method.
        animal.cry(); //This method can still be used normally
        System.out.println("------------------------");

        //If the subclass object becomes the parent class object after upcasting, then it can also be downcasted and converted back to the subclass object again
        Dog dog = (Dog) animal;
        dog.bark();

    }

}

The result of running the main method is as follows:

i am an animal ,i can cry
------------------------
i am dog ,i can bark , wang wang wang ! The transformation is successful, and the dog's bark method can also be executed!)


The following tests test for transformation (without any premise, the reference of the subclass points directly to the object of the superclass):

//The reference of the downcast subclass points to the object of the parent class. The object of the parent class is treated as a subclass, and the forced conversion is required!
        
//The following code, no error is reported when compiling, and an exception will be reported as soon as it runs
        Dog dog2 = (Dog) new Animal(); //The exception is: Exception in thread "main" java.lang.ClassCastException: PrimaryTest.Animal cannot be cast to PrimaryTest.Dog //It is easy to understand, we can say that a dog is a species, but it cannot be said that the animal is a dog.


And if it is written like this, it will not report an exception

if (new Animal() instanceof Dog){
            Dog dog2 = (Dog) new Animal();
            dog2.bark();

        }
But after dog2 executes the method, the result is that there is no program and it ends. Strictly speaking, java does not support such direct downcasting! (Looking at many people's blogs, downcasting is more convenient to use in generics)





Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325626200&siteId=291194637
Recommended