Advertisement
Guest User

Untitled

a guest
Sep 21st, 2017
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.55 KB | None | 0 0
  1.  
  2. public class Driver {
  3. public static void main(String[] args) {
  4.  
  5. Pet[] myArray = new Pet[3];
  6.  
  7. Dog Fido = new Dog("Fido");
  8. Cat Toby = new Cat("Toby");
  9. Dog Henry = new Dog("Henry");
  10.  
  11. myArray[0] = Fido;
  12. myArray[1] = Toby;
  13. myArray[2] = Henry;
  14.  
  15. for (int i = 0; i < myArray.length; i++) {
  16. if (myArray[i] instanceof Dog) {
  17. System.out.println(((Dog) myArray[i]).fetch());
  18. }
  19.  
  20. }
  21. for (int i=0; i<myArray.length; i++){
  22. if (myArray[i] instanceof Cat){
  23. System.out.println(((Pet)myArray[i]).getName());
  24. }
  25. }
  26. }
  27. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement