Home > Java > javaTutorial > body text

What is the concept of downward transformation in java

王林
Release: 2023-04-30 15:40:06
forward
1930 people have browsed it

Concept

1. Downward transformation is the conversion of parent class objects into subclass objects. We give a reference of the Animal type from the parent class to a Bird type reference. This is a downward transformation.

2. The format is

子类 子类对象=(子类)父类实例
Copy after login

Note

Be sure to perform forced type conversion when converting downward

Example

class Animal {
    public String name;
    public void eat() {
        System.out.println(this.name + " 正在吃");
    }
}
class Cat extends Animal {
 
}
class Bird extends Animal {
    public int age;
    public void fly() {
        System.out.println(this.name+"起飞");
    }
}
public class Test extends TestDemo {
    public static void main(String[] args) {
        Animal animal = new Animal();
        Bird bird = (Bird) animal;//必须进行强制类型转换
    }
}
Copy after login

The above is the detailed content of What is the concept of downward transformation in java. 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