Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
8.2 Class Members
The members of a class type are all of the following:
- Members inherited from its direct superclass (§8.1.3), except in class
Object, which has no direct superclass - Members inherited from any direct superinterfaces (§8.1.4)
- Members declared in the body of the class (§8.1.5)
Members of a class that are declared private are not inherited by subclasses of that class. Only members of a class that are declared protected or public are inherited by subclasses declared in a package other than the one in which the class is declared.
Constructors and static initializers are not members and therefore are not inherited.
The example:
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
}
}
causes four compile-time errors:
- An error occurs because
ColoredPointhas no constructor declared with two integer parameters, as requested by the use inmain. This illustrates the fact thatColoredPointdoes not inherit the constructors of its superclassPoint. - Another error occurs because
ColoredPointdeclares no constructors, and therefore a default constructor for it is automatically created (§8.6.7), and this default constructor is equivalent to: ColoredPoint() { super(); }
which invokes the constructor, with no arguments, for the direct superclass of the class ColoredPoint. The error is that the constructor for Point that takes no arguments is private, and therefore is not accessible outside the class Point, even through a superclass constructor invocation (§8.6.5).
- Two more errors occur because the method
resetof classPointisprivate, and therefore is not inherited by classColoredPoint. The method invocations in methodclearof classColoredPointand in methodmainof classTestare therefore not correct.