Name Inheritance Worksheet (Review) Questions 1-27: Consider the following hierarchy of classes. Animal <abstract> Dog Mammal Reptile <abstract> <abstract> Cat Lizard Snake Assume that Animal, Mammal, and Reptile are abstract classes, and that Dog, Cat, Lizard and Snake are non-abstract classes. Assume that all classes have a default constructor. Label each statement as valid or invalid. If invalid, tell why. 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17. 18. 19. 20. Animal a1 = new Animal(); Animal a2 = new Mammal(); Animal a3 = new Dog(); Animal a5 = Reptile(); Animal a6 = new Lizard(); Mammal m1 = new Mammal(); Mammal m2 = new Dog(); Reptile r1 = new Snake(); Reptile r2 = new Animal(); Dog d1 = new Dog(); Dog d2 = new Mammal(); Cat c3 = new Animal(); Lizard t1 = new Lizard(); Lizard t2 = new Reptile(); Snake s1 = new Snake(); Snake s3 = new Animal(); Snake s4 = new Dog(); Mammal m3 = new Lizard(); Lizard t3 = new Snake(); Reptile r3 = new Mammal(); Assume now that the Mammal class has a method called speak that returns the string “noise” and that the Cat class overrides that method and it returns the string “meow”. Tell if the following are valid or invalid and if valid, what is the value returned by the call? 21. 22. Dog d1 = new Dog(); d1.speak(); Animal a1 = new Cat(); a1.speak(); AP Computer Science 2/12/2016 Inheritance Review 23. 24. 25. 26. 27. Mammal m1 = new Cat(); m1.speak(); Cat c1 = new Cat(); c1.speak(); Cat c2 = new Mammal(); c2.speak(); Mammal m2 = new Mammal(); m2.speak(); Mammal m3 = new Dog(); m3.speak(); True/False 28. 29. 30. 31. 32. 33. You can instantiate objects using constructors from an abstract class. i.e. : AbstractClassType a = new AbstractClssType(); A variable of type Object can reference any type of object. Casting is necessary when a general typed variable is referencing a specific typed object and the programmer wants to invoke a method that is only defined in the specific type. A subclass may override a super class's methods, but not overload these methods. Inheritance is used when the relationship between 2 classes is a "Has-A" relationship. A class, other than Object, extends exactly one other class. 34. Given the following, what is the first thing that will happen when the constructor Bar() is called? public class Foo { private int iMyVal; public Foo() { iMyVal = 12; } } public class Bar { private String myName; public Bar() { myName = "unknown";} } AP Computer Science 2/12/2016 Inheritance Review Questions 35-37: Consider the following classes. public class Alpha { private int num; public Alpha(int n) { num = n; } public int get() { return num; } public void set(int x) { num = x; } public void mystery() { set(num + 22); System.out.println(num); } public class Beta extends Alpha { public Beta(int n) { super(2 * n); } public void set(int x) { super.set(x - 8); } public void mystery() { super.mystery(); System.out.println(get() + 2); } } } 35. What is the output of the following code segment? Alpha b = new Beta(9); System.out.println(b.get()); b.set(12); System.out.println(b.get()); 36. What is the output of the following code segment? Alpha a = new Alpha(17); a.mystery(); 37. What is the output of the following code segment? Alpha b = new Beta(100); b.mystery(); AP Computer Science 2/12/2016 Inheritance Review Questions 38-41: Consider the following classes. public class ClassOne { public void methodOne() { methodTwo(); System.out.print("1"); } public void methodTwo() { System.out.print("2"); } } public class ClassTwo extends ClassOne { public void methodOne() { System.out.print("3"); super.methodOne(); } public void methodTwo() { System.out.print("4"); super.methodTwo(); } } Assume the following declarations have been made. ClassOne c1 = new ClassOne(); ClassOne c2 = new ClassTwo(); 38. What is the output of the following statement? c1.methodOne(); 39. What is the output of the following statement? c1.methodTwo(); 40. What is the output of the following statement? c2.methodOne(); 41. What is the output of the following statement? c2.methodTwo(); AP Computer Science 2/12/2016 Inheritance Review