15.11.2
Compile Time Step 2: Determine Method Signature
EXPRESSIONS
/**
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
:
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 application 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 parameter, and the parameter is of type
Point
. This signature
becomes part of the binary representation of class
Test
produced by the 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
ColoredPoint
:
public void adopt(ColoredPoint p) {
adopt((Point)p); color = p.color;
}
330
footer
Our partners:
PHP: Hypertext Preprocessor Best Web Hosting
Java Web Hosting
Inexpensive Web Hosting
Jsp Web Hosting
Cheapest Web Hosting
Jsp Hosting
Cheap Hosting
Visionwebhosting.net Business web hosting division of Web
Design Plus. All rights reserved