Simple understanding and usage of Java up-casting and down-casting

 

Other online information about upward transformation and downward transformation cannot be completely understood by readers, so I decided to rewrite a tutorial to make it easy for everyone to understand.

Upward transformation: transform the sub-category into the parent category.

Downcast: Convert the parent class to the child class.

Inheritance preparation
        Let's take the reader step by step through examples:

        The transformation is based on inheritance, so first implement a subclass to inherit the parent class.

/**
 * 父类
 */
class Father {
    String name = "爸爸";
    public void sleep() {//睡觉方法
        System.out.println(name + "睡觉");
    }
}
/**
 * 子类继承父类
 */
class Son extends Father {
    String name = "儿子";
    public void p() {//调皮方法
        System.out.println(name + "调皮");
    }
}


Expression form
Upward transformation

public class UpAndDown {
    public static void main(String[] args) {
        // 向上转型:将父类引用指向子类对象
        Father f = new Son();
        f.sleep();//输出“爸爸睡觉”
      
        //如果f.p();编译出错,不可执行。因为p()不是Father的方法。
    }
}


        Although f is obtained by the up-transformation of the Son object, because it belongs to the Father class at this time and does not implement the p() method of the subclass, fp() cannot be used; it can be regarded as a kind of "slimming" at this time.

Down-casting
        is still the above example, if it is forced to be a sub-category. code show as below.

public class UpAndDown {
    public static void main(String[] args) {
        // 向下转型
        Father f = new Son();
        ((Son)f).P();//输出“儿子调皮”
    }
}


        Was it a pleasant surprise? The parent class is transformed into a subclass, and the unique methods of the subclass are restored.

        So, what if we are directly transitioning downwards?

 

public class UpAndDown {
    public static void main(String[] args) {
        // 直接向下转型
        Father f = new Father();
        f.sleep();//输出“爸爸睡觉”
        ((Son)f).P();//报错!!!!!!!!!!
    }
}


It seems that down-casting does not delay the call of the parent method.

However, the direct downward transformation will report an error! The prompt is as follows:

Exception in thread "main" java.lang.ClassCastException: lanqiao.Father cannot be cast to lanqiao.Son
    at lanqiao.UpAndDown.main(UpAndDown.java:7)

It means: Type conversion is abnormal, Father cannot be converted to Son.

We can understand this phenomenon in layman's terms:

Up-casting can be used as a way to hide itself, so turning it back (down-casting) method will return to its original state. Should it be yours or yours.
Direct downward transformation, the parent category does not have the unique method of the child category, so even if the transformation is successful, it is still a disabled son. Don't even think about it if it's not you.
 Actually, it's like this:

In memory, because the upcast refers to the parent class’s reference to the object of the subclass, it only points to the properties and methods that the parent class should have, while the unique methods and properties of the subclass do not point to (or say Hidden), when forced to the sub-category. It points to the subclass object again, and the things that belong to it are restored.

Come to the picture:

        Okay, so what is the purpose of transformation?

The use of up-casting and down-casting
Up-casting
We understand in this way that everyone can dance, and it is different for men and women to dance. (Women's ladies, please be merciful!)

Pretend to have code:

Suppose that Person has a dance() method.

Man extends Person      Women extends Person

At this time, both Man and Women rewrote Person's dance();

When Person p = new Man(); p.dance(); //This time is to call the dance method of men.
When Person p = new Woman(); p.dance();// This time is to call women’s dance The dance method

This is actually a java inheritance, polymorphism. It is conducive to program expansion. You need to understand this design method, which will allow you to write more maintainable and concise code.

Downward transformation,
not enough inspiration, copy it together: (Don’t beat me)

https://blog.csdn.net/xyh269/article/details/52231944

 

Guess you like

Origin blog.csdn.net/qq_34159161/article/details/105065738