abstract class Animal { String name; // The human name for the animal, not some secret animal name. String sound; // What sound does it vocalize? int age; /** Constructor. * @param _name the name of the animal. * @param _sound what sound the animal makes. */ Animal( String _name, String _sound ) { name = _name; sound = _sound; age = 0; } public int getAge() { return this.age; } public void setAge( int _age ) { this.age = _age; } public String toString() { return this.name + " says " + this.sound; } /** When the animal wants to bug a human, it says... * @return The sound of the animal, bugging its owner. */ public String speak() { return name + ": " + sound + ", " + sound + "."; } /** Test: overriding "speak". */ public static void test2() { System.out.println( "====" ); System.out.println( "Testing Inheritance:" ); System.out.println( new Cat("Bartok").speak() ); System.out.println( new Dog("Beethoven").wagTail() ); System.out.println( new Dog("Phydeaux").speak() ); System.out.println( new Python().speak() ); } /** Test: polymorphism. */ public static void test4() { System.out.println( "====" ); System.out.println( "Testing polymorphism (w/ small dogs): " ); Animal[] zoo = { new Cat("Bartok"), new Dog("Beethoven"), new SmallDog("Phydeaux"), new Python() }; // Demonstrate polymorphism: // In the loop, "critter" is of (declared) type Animal, // yet Dog's specialized speak() is still getting used. // System.out.println( "Polymorphism:" ); for ( Animal critter : zoo ) { System.out.println( critter.speak() ); } } }