Java Reference
In-Depth Information
this.color = (byte)color;
}
/** Copy all relevant fields of the argument into
this ColoredPoint object. */
public void adopt(Point p) { x = p.x; y = p.y; }
public String toString() {
String s = "," + COLORS[color];
return super.toString(s);
}
}
Now consider a third compilation unit that uses ColoredPoint :
Click here to view code image
import points.*;
class Test {
public static void main(String[] args) {
ColoredPoint cp =
new ColoredPoint(6, 6, ColoredPoint.RED);
ColoredPoint cp2 =
new ColoredPoint(3, 3, ColoredPoint.GREEN);
cp.adopt(cp2);
System.out.println("cp: " + cp);
}
}
The output is:
cp: (3,3,red)
The programmer who coded class Test has expected to see the word green , because the
actual argument, a ColoredPoint , has a color field, and color would seem to be a “relevant
field”. (Of course, the documentation for the package points ought to have been much
more precise!)
Notice, by the way, that the most specific method (indeed, the only applicable method)
for the method invocation of adopt has a signature that indicates a method of one para-
meter, and the parameter is of type Point . This signature becomes part of the binary
representation of class Test produced by the Java compiler and is used by the method
invocation at run time.
Suppose the programmer reported this software error and the maintainer of the points
package decided, after due deliberation, to correct it by adding a method to class Co-
loredPoint :
public void adopt(ColoredPoint p) {
Search WWH ::




Custom Search