Java - on the upward transformation and downward transformation of subclass parent class

The following Animal is the parent class, Dog and Cat are the children, respectively inheriting Animal

class Animal{
    public void shout(){
        System.out.println("Give a cry.");
    }
}

class Dog extends Animal{
    public void shout() {
        System.out.println("Wangwangwang");
    }
    public void seeDoor() {
        System.out.println("Gatekeeper");
    }
}

class Cat extends Animal{
    public void shout() {
        System.out.println("cat");
    }
}

 

Conclusion:

1. It is feasible to convert a dog object into an Animal object, such as d2

It is feasible to convert Cat objects into Animal objects, such as c2

2. It is feasible to convert D2 as an Animal object into a Dog object. Such as d3, and can execute a Dog specific method

3. c2 is converted as an Animal object to a Dog object. After compilation, it runs and reports an error. java.lang.ClassCastException is displayed. The type conversion is wrong

 

Instance code:

1. It is feasible to convert a Dog object into an Animal object, such as d2

Cat objects can be converted into Animal objects, such as c2

public class TestPoym{
    public static void main(String[] args) {
        Dog d1 = new Dog();
        animalCry(d1);
        Animal d2 = new Dog();  //Upward transformation
        animalCry(d2);

      Animal c1 = new Cat();
      animalCry(c1);
      Animal c2 = new Cat();
      animalCry(c2);

    }
    
    static void animalCry(Animal a) {
        a.shout();
    }
}

Result:

2. It is feasible to convert d2 as an Animal object into a Dog object. Such as d3, and can implement a Dog specific method, gatekeeper

public class TestPoym{
    public static void main(String[] args) {
        Dog d1 = new Dog();
        animalCry(d1);
        Animal d2 = new Dog();  //Upward transformation
        animalCry(d2);
        Animal c1 = new Cat();
        animalCry(c1);
        Animal c2 = new Cat();
        animalCry(c2);
        
        Dog d3 = (Dog)d2;  //Downward transformation
        d3.seeDoor();
        
    }
    
    static void animalCry(Animal a) {
        a.shout();
    }
}

Result:

3. c2 is converted as an Animal object to a Dog object. After compilation, an error is reported when running. java.lang.ClassCastException is displayed. The type conversion is in error

Code:

public class TestPoym{
    public static void main(String[] args) {
        Dog d1 = new Dog();
        animalCry(d1);
        Animal d2 = new Dog();  //Upward transformation
        animalCry(d2);
        Animal c1 = new Cat();
        animalCry(c1);
        Animal c2 = new Cat();
        animalCry(c2);
        
        Dog d3 = (Dog)d2;  //Downward transformation
        d3.setDoor();
        
        Dog c3 = (Dog)c2;  //Compile passed, run error, show cast error
    }
    
    static void animalCry(Animal a) {
        a.shout();
    }
}

Result:

Tags: Java

Posted on Sun, 01 Dec 2019 17:28:33 -0800 by ilangovanbe2005