Advertisement
Guest User

Untitled

a guest
Apr 7th, 2020
190
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.58 KB | None | 0 0
  1. abstract class Animal {
  2.    public abstract void noise();
  3. }
  4.  
  5. class Dog extends Animal {
  6.  
  7.    public void noise() {
  8.       System.out.println("Woof!");
  9.    }
  10. }
  11. class Cat extends Animal {
  12.  
  13.    public void noise() {
  14.       System.out.println("Nyan");
  15.    }
  16. }
  17.  
  18. public class Test {
  19.    public static void main(String[] args) {
  20.       Dog rover = new Dog();
  21.       Cat roverCat = convert(rover);
  22.       roverCat.noise();
  23.    }
  24.  
  25.    public static Cat convert(Dog d) {
  26.       Cat[] cats = new Cat[100];
  27.       Animal[] animals = cats;
  28.       animals[0] = d;
  29.       return cats[0];
  30.    }
  31. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement