和继承一样,这章主要讲解多态和接口的编程思想,而习题并不难(相比数组那一章)。
Self-Review Exercises 10.1 Fill in the blanks in each of the following statements: a) If a class contains at least one abstract method, it’s a(n) abstract class. b) Classes from which objects can be instantiated are called concreate classes. c) Polymorphism involves using a superclass variable to invoke methods on superclass and subclass objects, enabling you to “program in the general.” d) Methods that are not interface methods and that do not provide implementations must be declared using keyword abstract. e) Casting a reference stored in a superclass variable to a subclass type is called downcasting. 10.2 State whether each of the statements that follows is true or false. If false, explain why. a) All methods in an abstract class must be declared as abstract methods. (F) b) Invoking a subclass-only method through a subclass variable is not allowed. (F) c) If a superclass declares an abstract method, a subclass must implement that method. (F) d) An object of a class that implements an interface may be thought of as an object of that interface type. (T) 10.3 (Java SE 8 interfaces) Fill in the blanks in each of the following statements: a) In Java SE 8, an interface may declare —default methods, that is, public methods with concrete implementations that specify how an operation should be performed. b) As of Java SE 8, interfaces can now include static helper methods. c) As of Java SE 8, any interface containing only one method is known as a(n) functional interface. Exercises 10.4 How does polymorphism enable you to program “in the general” rather than “in the specific”? Discuss the key advantages of programming “in the general.” 10.5 What are abstract methods? Describe the circumstances in which an abstract method would be appropriate. 10.6 How does polymorphism promote extensibility? 10.7 Discuss three proper ways in which you can assign superclass and subclass references to variables of superclass and subclass types. 10.8 Compare and contrast abstract classes and interfaces. Why would you use an abstract class? Why would you use an interface? 10.9 (Java SE 8 Interfaces) Explain how default methods enable you to add new methods to an existing interface without breaking the classes that implemented the original interface. 10.10 (Java SE 8 Interfaces) What is a functional interface? 10.11 (Java SE 8 Interfaces) Why is it useful to be able to add static methods to interfaces? 10.12 (Payroll System Modification) Modify the payroll system of Figs. 10.4–10.9 to include private instance variable birthDate in class Employee. Use class Date of Fig. 8.7 to represent an employee’s birthday. Add get methods to class Date. Assume that payroll is processed once per month. Create an array of Employee variables to store references to the various employee objects. In a loop, calculate the payroll for each Employee (polymorphically), and add a $100.00 bonus to the person’s payroll amount if the current month is the one in which the Employee’s birthday occurs. 10.13 (Project: Shape Hierarchy) Implement the Shape hierarchy shown in Fig. 9.3. Each Two-DimensionalShape should contain method getArea to calculate the area of the two-dimensional shape. Each ThreeDimensionalShape should have methods getArea and getVolume to calculate the surface area and volume, respectively, of the three-dimensional shape. Create a program that uses an array of Shape references to objects of each concrete class in the hierarchy. The program should print a text description of the object to which each array element refers. Also, in the loop that processes all the shapes in the array, determine whether each shape is a TwoDimensionalShape or a ThreeDimensionalShape. If it’s a TwoDimensionalShape, display its area. If it’s a ThreeDimensionalShape, display its area and volume. 10.14 (Payroll System Modification) Modify the payroll system of Figs. 10.4–10.9 to include an additional Employee subclass PieceWorker that represents an employee whose pay is based on the number of pieces of merchandise produced. Class PieceWorker should contain private instance variables wage (to store the employee’s wage per piece) and pieces (to store the number of pieces produced). Provide a concrete implementation of method earnings in class PieceWorker that calculates the employee’s earnings by multiplying the number of pieces produced by the wage per piece. Create an array of Employee variables to store references to objects of each concrete class in the new Employee hierarchy. For each Employee, display its String representation and earnings. 10.15 (Accounts Payable System Modification) In this exercise, we modify the accounts payable application of Figs. 10.11–10.15 to include the complete functionality of the payroll application of Figs. 10.4–10.9. The application should still process two Invoice objects, but now should process one object of each of the four Employee subclasses. If the object currently being processed is a Base- PlusCommissionEmployee, the application should increase the BasePlusCommissionEmployee’s base salary by 10%. Finally, the application should output the payment amount for each object. Complete the following steps to create the new application: a) Modify classes HourlyEmployee (Fig. 10.6) and CommissionEmployee (Fig. 10.7) to place them in the Payable hierarchy as subclasses of the version of Employee (Fig. 10.13) that implements Payable. [Hint: Change the name of method earnings to getPaymentAmount in each subclass so that the class satisfies its inherited contract with interface Payable.] b) Modify class BasePlusCommissionEmployee (Fig. 10.8) such that it extends the version of class CommissionEmployee created in part (a). c) Modify PayableInterfaceTest (Fig. 10.15) to polymorphically process two Invoices, one SalariedEmployee, one HourlyEmployee, one CommissionEmployee and one Base- PlusCommissionEmployee. First output a String representation of each Payable object. Next, if an object is a BasePlusCommissionEmployee, increase its base salary by 10%. Finally, output the payment amount for each Payable object. 10.16 (Accounts Payable System Modification) It’s possible to include the functionality of the payroll application (Figs. 10.4–10.9) in the accounts payable application without modifying Employee subclasses SalariedEmployee, HourlyEmployee, CommissionEmployee or BasePlusCommission- Employee. To do so, you can modify class Employee (Fig. 10.4) to implement interface Payable and declare method getPaymentAmount to invoke method earnings. Method getPaymentAmount would then be inherited by the subclasses in the Employee hierarchy. When getPaymentAmount is called for a particular subclass object, it polymorphically invokes the appropriate earnings method for that subclass. Reimplement Exercise 10.15 using the original Employee hierarchy from the payroll application of Figs. 10.4–10.9. Modify class Employee as described in this exercise, and do not modify any of class Employee’s subclasses. Making a Difference 10.17 (CarbonFootprint Interface: Polymorphism) Using interfaces, as you learned in this chapter, you can specify similar behaviors for possibly disparate classes. Governments and companies worldwide are becoming increasingly concerned with carbon footprints (annual releases of carbon dioxide into the atmosphere) from buildings burning various types of fuels for heat, vehicles burning fuels for power, and the like. Many scientists blame these greenhouse gases for the phenomenon called global warming. Create three small classes unrelated by inheritance—classes Building, Car and Bicycle. Give each class some unique appropriate attributes and behaviors that it does not have in common with other classes. Write an interface CarbonFootprint with a getCarbonFootprint method. Have each of your classes implement that interface, so that its getCarbonFootprint method calculates an appropriate carbon footprint for that class (check out a few websites that explain how to calculate carbon footprints). Write an application that creates objects of each of the three classes, places references to those objects in ArrayList<CarbonFootprint>, then iterates through the Array- List, polymorphically invoking each object’s getCarbonFootprint method. For each object, print some identifying information and the object’s carbon footprint. |