Java Reference
In-Depth Information
c)
A Car class has an is-a relationship with the SteeringWheel and Brakes classes.
d)
When a subclass redefines a superclass method by using the same signature, the subclass
is said to overload that superclass method.
Answers to Self-Review Exercises
9.1 a) Inheritance. b) public and protected . c) is-a or inheritance. d) has-a or composition.
e) hierarchical. f) public . g) constructor. h) super .
9.2 a) True. b) False. A has-a relationship is implemented via composition. An is-a relationship
is implemented via inheritance. c) False. This is an example of a has-a relationship. Class Car has an
is-a relationship with class Vehicle . d) False. This is known as overriding, not overloading—an
overloaded method has the same name, but a different signature.
Exercises
9.3 (Using Composition Rather Than Inheritance) Many programs written with inheritance
could be written with composition instead, and vice versa. Rewrite class BasePlusCommissionEm-
ployee (Fig.9.11) of the CommissionEmployee - BasePlusCommissionEmployee hierarchy to use
composition rather than inheritance.
9.4 (Software Reuse) Discuss the ways in which inheritance promotes software reuse, saves time
during program development and helps prevent errors.
9.5 ( Student Inheritance Hierarchy) Draw an inheritance hierarchy for students at a university
similar to the hierarchy shown in Fig. 9.2. Use Student as the superclass of the hierarchy, then ex-
tend Student with classes UndergraduateStudent and GraduateStudent . Continue to extend the hi-
erarchy as deep (i.e., as many levels) as possible. For example, Freshman , Sophomore , Junior and
Senior might extend UndergraduateStudent , and DoctoralStudent and MastersStudent might be
subclasses of GraduateStudent . After drawing the hierarchy, discuss the relationships that exist be-
tween the classes. [ Note: You do not need to write any code for this exercise.]
9.6 ( Shape Inheritance Hierarchy) The world of shapes is much richer than the shapes included
in the inheritance hierarchy of Fig. 9.3. Write down all the shapes you can think of—both two-di-
mensional and three-dimensional—and form them into a more complete Shape hierarchy with as
many levels as possible. Your hierarchy should have class Shape at the top. Classes TwoDimension-
alShape and ThreeDimensionalShape should extend Shape . Add additional subclasses, such as Quad-
rilateral and Sphere , at their correct locations in the hierarchy as necessary.
9.7 ( protected vs. private ) Some programmers prefer not to use protected access, because
they believe it breaks the encapsulation of the superclass. Discuss the relative merits of using pro-
tected access vs. using private access in superclasses.
9.8 ( Quadrilateral Inheritance Hierarchy) Write an inheritance hierarchy for classes Quadri-
lateral , Trapezoid , Parallelogram , Rectangle and Square . Use Quadrilateral as the superclass
of the hierarchy. Create and use a Point class to represent the points in each shape. Make the hier-
archy as deep (i.e., as many levels) as possible. Specify the instance variables and methods for each
class. The private instance variables of Quadrilateral should be the x-y coordinate pairs for the
four endpoints of the Quadrilateral . Write a program that instantiates objects of your classes and
outputs each object's area (except Quadrilateral ).
9.9
(What Does Each Code Snippet Do?)
a)
Assume that the following method call is located in an overridden earnings method in
a subclass:
super .earnings()
 
Search WWH ::




Custom Search