Java Reference
In-Depth Information
Latebindingisusedforcallstonon- final instancemethods.Forallothermethod
calls,thecompilerknowswhichmethodtocall,andinsertsaninstructionintothecom-
piledcodethatcallsthemethodassociatedwiththevariable'stype(notitsvalue).This
task is known as early binding .
Youcanalsoupcastfromonearraytoanotherprovidedthatthearraybeingupcastis
a subtype of the other array. Consider Listing 2-35 .
Listing 2-35. Demonstrating array upcasting
class Point
{
private int x, y;
Point(int x, int y)
{
this.x = x;
this.y = y;
}
int getX() { return x; }
int getY() { return y; }
}
class ColoredPoint extends Point
{
private int color;
ColoredPoint(int x, int y, int color)
{
super(x, y);
this.color = color;
}
int getColor() { return color; }
}
class UpcastArrayDemo
{
public static void main(String[] args)
{
ColoredPoint[] cptArray = new ColoredPoint[1];
cptArray[0] = new ColoredPoint(10, 20, 5);
Point[] ptArray = cptArray;
 
Search WWH ::




Custom Search