Java Reference
In-Depth Information
We use the phrase
the type of a member
to denote:
• For a field, its type.
• For a method, an ordered 3-tuple consisting of:
♦ argument types: a list of the types of the arguments to the method member.
♦ return type: the return type of the method member.
♦ throws clause: exception types declared in the throws clause of the method
member.
Fields, methods, and member types of a class type may have the same name, since they
However, this is discouraged as a matter of style.
Example 8.2-1. Use of Class Members
class Point {
int x, y;
private Point() { reset(); }
Point(int x, int y) { this.x = x; this.y = y; }
private void reset() { this.x = 0; this.y = 0; }
}
class ColoredPoint extends Point {
int color;
void clear() { reset(); } // error
}
class Test {
public static void main(String[] args) {
ColoredPoint c = new ColoredPoint(0, 0); // error
c.reset(); // error
}
}
This program causes four compile-time errors.
One error occurs because
ColoredPoint
has no constructor declared with two
int
para-
meters, as requested by the use in
main
. This illustrates the fact that
ColoredPoint
does
not inherit the constructors of its superclass
Point
.
Another error occurs because
ColoredPoint
declares no constructors, and therefore a de-
fault constructor for it is automatically created (§
8.8.9
), and this default constructor is
equivalent to: