1.1 Given the Animal class, fill in the definition of the Cat class so that when greet() is called, the label “Cat” (instead of “Animal”) is printed to the screen. Assume that a Cat will make a “Meow!” noise if the cat is 5 years or older and “MEOW!” if the cat is less than 5 years old.
publicvoidplayFetch() { System.out.println("Fetch, " + name + "!"); } }
Consider what would happen if we added the following to the bottom of main under line 12:
1 2
a = newDog("Spot", 10); d = a;
The second line will cause a compile-time error, because d’s static type is dog, while a’s static type is animal, animal may not be dog.
Why would this code produce a compiler error? How could we fix this error? we can make a type cast for a, which is d = (Dog)a;
3 An Exercise in Inheritance Misery Extra
3.1 Cross out any lines that cause compile-time errors or cascading errors (failures that occur because of an error that happened earlier in the program), and put an X through runtime errors (if any). Don’t just limit your search to main, there could be errors in classes A,B,C. What does D.main output after removing these lines?