/** An instance is a zoo creature. PAGE ONE OF CS1110 REVIEW SESSION, apparent/real classes etc. */ public class Creature { /** This Creature's name */ private String name; /** set of Creatures in the zoo*/ // this field has been set to public to make this running example // shorter to present public static Creature[] zoo; /** Constructor: a new Creature with name n */ public Creature(String n) { setName(n); } /** = this Creature's name */ public String getName() { return name; } /** Set this Creature's name to n */ public void setName(String n) { name= n; } } /** An instance is a bird */ public class Bird extends Creature { /** set of Birds in the zoo. * (Elements should also be listed in the zoo) */ // public for the purposes of example public static Bird[] aviary; /** Constructor: a Bird with name n */ public Bird(String n) { super(n); } /** = "a Bird can (usually) fly" */ public boolean canFly() { return true; } } /** An instance is a penguin */ public class Penguin extends Bird{ /** Constructor: a new Penguin with name n*/ public Penguin(String n) { super(n); } /** = "a Penguin can usually fly" */ public boolean canFly() { return false; } public static void demo() { // PAGE TWO OF CS1110 REVIEW SESSION #2 HANDOUT // (1) local variables Creature x; Bird y; Penguin z; // (2) Can you create a new Bird named Tweety and store its name in x? // (3) Can you store (the name of) the new Bird just created in y? // (4) Can you create a new Penguin named Opus and store (the name of) it in z? // (5) Can you execute this statement? // zoo[0]=x; // (6) Can you make zoo able to hold 3 elements, and aviary able to hold 2? // (7) Can you execute this statement (now)? // zoo[0]=x; // (8) How many ways are there to store the object named in x in aviary[0] (IN THIS // PARTICULAR SETTING, WITH x, y, z AS SET ABOVE)? // (9) Can you store the object named in z in both zoo[1] and aviary[1]? // (10) What happens when we do this? // x.setName("daffy"); // System.out.println("aviary[0] after x.setName(\"daffy\"): " + aviary[0].getName()); // (11) What happens when we (try to) do each of these things? // (11a) // System.out.println("can aviary[0] (codename " + aviary[0].getName() + ") fly? " + aviary[0].canFly()); // (11b) // System.out.println("can aviary[1] (codename " + aviary[1].getName() + ") fly? " + aviary[1].canFly()); // (11c) // System.out.println("can zoo[0] (codename " + zoo[0].getName() + ") fly? " + zoo[0].canFly()); // (11d) // System.out.println("can ((Bird) zoo)[0] (codename " + zoo[0].getName() + ") fly? " + ((Bird) zoo[0]).canFly()); } }