Exercises

9.3Many programs written with inheritance could be written with composition instead, and vice versa. Rewrite class BasePlusCommissionEmployee4 (Fig. 9.13) of the CommissionEmployee3BasePlusCommissionEmployee4 hierarchy to use composition rather than inheritance. After you do this, assess the relative merits of the two approaches for the CommissionEmployee3 and BasePlusCommissionEmployee4 problems, as well as for object-oriented programs in general. Which approach is more natural? Why?
9.4Discuss the ways in which inheritance promotes software reuse, saves time during program development and helps prevent errors.
9.5Draw an inheritance hierarchy for students at a university similar to the hierarchy shown in Fig. 9.2. Use Student as the ...

Get Java™ How to Program, Seventh Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.