Page 588 - Introduction to Programming with Java: A Problem Solving Approach
P. 588

                554 Chapter 13 Inheritance and Polymorphism
11. [after §13.9] Rewrite the Commission interface shown in Figure 13.14 to explicitly show the abstract, public, static, and final modifiers in every place where they apply. (Your elaborated interface definition should compile.)
12. [after §13.9] Change the Pets3 class in Exercise 10, above, as follows: Replace all instances of Pets3 by Pets4, and replace Animal2 by Animal3. Then, write an Animal3 interface and Dog3 and Cat3 classes that implement Animal3, so that when you run the Pets4 program and input either a ‘c’ or a ‘d,’ the program prints either “Meow! Meow!” or “Woof! Woof.”
13. [after §13.10] Expand the cryptic code in the getFICA method of the Employee3 class in Figure 13.20 into “if else” statements so that the algorithm is easier to understand.
Review Question Solutions
1. False. Every class is a descendant of the Object class, so specifying extends Object is not necessary. In fact it is undesirable, because it prevents extension of some other class.
2. True.
3. The equals method defined in the String class compares a string’s characters.
4. The Object class’s toString method returns a string concatenation of these three text components: • fullclassname
• @character
• ahexadecimalhashcodevalue
5. Nothing. It’s just a matter of style—whether you want more compactness or more self-documentation. Apago PDF Enhancer
6. True.
7. False. At runtime. The JVM determines which method is called.
8. To be able to assign one reference variable to the other one (without using a cast operator), the left-side variable’s class must be a(n) superclass/ancestor of the right-side reference variable’s class.
9. True, if each element’s type is either the type defined in the array declaration or a descendant of that type (or conforms to the interface that defines the array’s type—see Section 13.9).
10. The syntax features of an abstract method are:
• Themethodheadingcontainstheabstractmodifier. • Thereisasemicolonattheendoftheheading.
• Thereisnomethodbody.
11. True.
12. True.
13. True
14. True.
15. It is legal to access a protected member:
• fromwithinthesameclassastheprotectedmember
• fromwithinaclassdescendedfromtheprotectedmember • fromwithinthesamepackage
16. True. An abstract method must be public or protected (it cannot be private). An overriding method must be no more restrictive than its overridden method. Therefore, if a method overrides an abstract method, it cannot be private.
 







































































   586   587   588   589   590