Experiment O3.2, Step 3.
Because PointPrinter
's print
method
was designed to work with Point
s
and not NewPoint
s, Java won't let you call the
print
method on a NewPoint
.
Experiment O3.2, Step 4.
Since PointPrinter
needs to support both Point
s
and NewPoint
s, it makes sense to maintain the existing method
and add an additional method that differs only in that we use
NewPoint
instead of Point
. The method might
look like
/** * Print a point using a particular output object. */ public void print(SimpleOutput out, NewPoint pt) { out.println("(" + pt.getX() + "," + pt.getY() + ")"); out.println(" distance from origin: " + pt.distanceFromOrigin()); } // print(SimpleOutput, NewPoint)
Experiment O3.2, Step 5.
If you have done everything appropriately, compilation should be successful in
this step. Why? Before there was no print
method that
could accept a NewPoint
as a parameter. Now there is.
Experiment O3.3, Step 3.
In Experiment O3.2, you found that it is not
possible to pass a NewPoint
as a parameter to the
print
method of PointPrinter
. In
this experiment, you found that it is possible to pass an
ExtendedPoint
as a parameter to the same function
without modifying print
! This illustrates
the principle that you can use an element of a subclass wherever an
element of a superclass is called for.
Experiment O3.4, Step 3.
You will need to create the appropriate method using code similar to
that which appears in the discussion. In addition, you will need to
import the new Printable
class.
[Front Door] [Introduction] [Code]
Copyright (c) 1998 Samuel A. Rebelsky. All rights reserved.
Source text last modified Tue Oct 26 11:24:17 1999.
This page generated on Tue Oct 26 15:36:14 1999 by Siteweaver.
Contact our webmaster at rebelsky@math.grin.edu