Home > Java > javaTutorial > body text

What does java polymorphic upward transformation refer to?

PHPz
Release: 2023-04-20 09:19:06
forward
803 people have browsed it

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;//向上转型
    }
}
Copy after login

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!

Related labels:
source:yisu.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template