Java Reference
In-Depth Information
Point() { super(); x = 1; y = 1; }
Therefore, the constructor for Object which takes no arguments is invoked.
The class Object has no superclass, so the recursion terminates here. Next, any instance
initializers and instance variable initializers of Object are invoked. Next, the body of
the constructor of Object that takes no arguments is executed. No such constructor is
declared in Object , so the Java compiler supplies a default one, which in this special
case is:
Object() { }
This constructor executes without effect and returns.
Next, all initializers for the instance variables of class Point are executed. As it hap-
pens, the declarations of x and y do not provide any initialization expressions, so no
action is required for this step of the example. Then the body of the Point constructor
is executed, setting x to 1 and y to 1 .
Next, the initializers for the instance variables of class ColoredPoint are executed. This
step assigns the value 0xFF00FF to color . Finally, the rest of the body of the ColoredPoint
constructor is executed (the part after the invocation of super ); there happen to be no
statements in the rest of the body, so no further action is required and initialization is
complete.
Example 12.5-2. Dynamic Dispatch During Instance Creation
Click here to view code image
class Super {
Super() { printThree(); }
void printThree() { System.out.println("three"); }
}
class Test extends Super {
int three = (int)Math.PI; // That is, 3
void printThree() { System.out.println(three); }
public static void main(String[] args) {
Test t = new Test();
t.printThree();
}
}
This program produces the output:
0
3
Search WWH ::




Custom Search