Explanation
1. Upcasting is to transfer a subclass reference to a parent class reference, that is, the parent class reference refers to the object of the subclass.
2. The format is parent class parent class object = subclass instance.
At this time, the method called through the parent class reference variable is the method of the subclass that overrides or inherits the parent class, not the method of the parent class. But the attribute called is still the attribute of the parent class.
Example
class Animal { public String name; public void eat() { System.out.println(this.name + " 正在吃"); } } class Cat extends Animal { } public class Test extends TestDemo { public static void main(String[] args) { //父类引用 引用了 子类引用所引用的对象 Cat cat = new Cat(); Animal animal = cat;//向上转型 } }
The above is the detailed content of What does java polymorphic upward transformation refer to?. For more information, please follow other related articles on the PHP Chinese website!