Home  >  Article  >  Java  >  What is the concept of downward transformation in java

What is the concept of downward transformation in java

王林
王林forward
2023-04-30 15:40:061931browse

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

子类 子类对象=(子类)父类实例

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;//必须进行强制类型转换
    }
}

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!

Statement:
This article is reproduced at:yisu.com. If there is any infringement, please contact admin@php.cn delete