Java Reference
In-Depth Information
b.
Shape b = r
;
c.
String c = r
;
d.
ActionListener d = r
;
e.
Measurable e = r
;
f.
Serializable f = r
;
g.
Object g = r
;
430
431
Exercise R9.7. Classes such as
Rectangle2D.Double
,
Ellipse2D.Double
and
Line2D.Double
implement the
Shape
interface. The
Shape
interface has a method
Rectangle getBounds()
that returns a rectangle completely enclosing the shape. Consider the
method call:
Shape s = . . .;
Rectangle r = s.getBounds();
Explain why this is an example of polymorphism.
Exercise R9.8. In Java, a method call such as
x.f()
uses late bindingȌ
the exact method to be called depends on the type of the object to which
x
refers. Give two kinds of method calls that use early binding in Java.
Exercise R9.9. Suppose you need to process an array of employees to find
the average and the highest salaries. Discuss what you need to do to use the
implementation of the
DataSet
class in
Section 9.1
(which processes
Measurable
objects). What do you need to do to use the second
implementation (in
Section 9.4
)? Which is easier?
Exercise R9.10. What happens if you add a
String
object to the
implementation of the
DataSet
class in
Section 9.1
? What happens if
you add a
String
object to a
DataSet
object of the implementation in
Section 9.4
that uses a
RectangleMeasurer
class?