Advertisement
thenewboston

Java Programming Tutorial - 61 - Simple Polymorphic Program

Aug 22nd, 2014
213
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.63 KB | None | 0 0
  1. public class apples {
  2.    public static void main(String[] args){
  3.      
  4.       Animal[] thelist = new Animal[2];
  5.       Dog d = new Dog();
  6.       Fish f = new Fish();
  7.      
  8.       thelist[0]=d;
  9.       thelist[1]=f;
  10.      
  11.       for(Animal x: thelist){
  12.          x.noise();
  13.       }
  14.    }
  15.  
  16. }
  17.  
  18. public class Dog extends Animal {
  19.    
  20.    public void noise(){
  21.       System.out.println("Ruff");
  22.    }
  23. }
  24.  
  25. public class Fish extends Animal{
  26.    
  27.    public void noise(){
  28.       System.out.println("Glurp Slurp");
  29.    }
  30. }
  31.  
  32. public class Animal {
  33.    
  34.    public void noise(){
  35.       System.out.println("Animal don´t make noise");
  36.    }
  37. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement