Java Reference
In-Depth Information
Superclass constructor invocations begin with either the keyword super (possibly
prefaced with explicit type arguments) or a Primary expression. They are used to
invoke a constructor of the direct superclass.
Superclass constructor invocations may be subdivided:
Unqualified superclass constructor invocations begin with the keyword super
(possibly prefaced with explicit type arguments).
Qualified superclass constructor invocations begin with a Primary expression.
They allow a subclass constructor to explicitly specify the newly created ob-
ject's immediately enclosing instance with respect to the direct superclass
8.1.3 ) . This may be necessary when the superclass is an inner class.
An explicit constructor invocation statement in a constructor body may not refer to any
instance variables or instance methods or inner classes declared in this class or any super-
class, or use this or super in any expression; otherwise, a compile-time error occurs.
The exception types that an explicit constructor invocation statement can throw are speci-
fied in § 11.2.2 .
Example 8.8.7.1-1. Qualified Superclass Constructor Invocation
Click here to view code image
class Outer {
class Inner {}
}
class ChildOfInner extends Outer.Inner {
ChildOfInner() { (new Outer()).super(); }
}
Example 8.8.7.1-2. Restrictions on Explicit Constructor Invocation Statements
If the first constructor of ColoredPoint in the example from § 8.8.7 were changed as fol-
lows:
Click here to view code image
class Point {
int x, y;
Point(int x, int y) { this.x = x; this.y = y; }
}
class ColoredPoint extends Point {
static final int WHITE = 0, BLACK = 1;
int color;
Search WWH ::




Custom Search