/** Pet.java - example of polymorphism * We're going to declare a variable pet of the Animal class. * But 'pet' will assume different dynamic types depending on which * constructor we use. */ public class Pet { public static void main(String [] args) { Animal pet; System.out.printf("animal #1...\n"); pet = new Bird(); pet.feed(); System.out.printf("\nanimal #2...\n"); pet = new Fish(); pet.feed(); // Let's make a Penguin. Even casting it as a Bird doesn't fool the // compiler. It's still a Penguin! System.out.printf("\nanimal #3...\n"); pet = new Penguin(); pet = (Bird) pet; pet.feed(); } }