The members of a class type are all of the following:
Object
, which has no direct superclass
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.
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:
ColoredPoint
has no constructor declared with two integer parameters, as requested by the use in main
. This illustrates the fact that ColoredPoint
does not inherit the constructors of its superclass Point
.
ColoredPoint
declares 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).
reset
of class Point
is private
, and therefore is not inherited by class ColoredPoint
. The method invocations in method clear
of class ColoredPoint
and in method main
of class Test
are therefore not correct.