CLASSES
Constructor Overloading
8.6.6
class ColoredPoint extends Point {
static final int WHITE = 0, BLACK = 1;
int color;
ColoredPoint(int x, int y) {
this(x, y, WHITE);
}
ColoredPoint(int x, int y, int color) {
super(x, y);
this.color = color;
}
}
the first constructor of
ColoredPoint
invokes the second, providing an additional
argument; the second constructor of
ColoredPoint
invokes the constructor of its
superclass
Point
, passing along the coordinates.
An explicit constructor invocation statement may not refer to any instance
variables or instance methods declared in this class or any superclass, or use
this
or
super
in any expression; otherwise, a compile time error occurs. For example,
if the first constructor of
ColoredPoint
in the example above were changed to:
ColoredPoint(int x, int y) {
this(x, y, color);
}
then a compile time error would occur, because an instance variable cannot be
used within a superclass constructor invocation.
An invocation of the constructor of the direct superclass, whether it actually
appears as an explicit constructor invocation statement or is provided automati
cally ( 8.6.7), performs an additional implicit action after a normal return of con
trol from the constructor: all instance variables that have initializers are initialized
at that time, in the textual order in which they appear in the class declaration. An
invocation of another constructor in the same class using the keyword
this
does
not perform this additional implicit action.
12.5 describes the creation and initialization of new class instances.
8.6.6 Constructor Overloading
Overloading of constructors is identical in behavior to overloading of methods.
The overloading is resolved at compile time by each class instance creation
expression ( 15.8).
179
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