Java Reference
In-Depth Information
ce to an object of Outer must be supplied when the Inner constructor is
invoked via super . For example:
class Unrelated extends Outer.Inner {
Unrelated(Outer ref) {
ref.super();
}
}
When the construction of an Unrelated object reaches the point where
the superclass constructor is invoked, there must be an object of class
Outer to which the superclass object can be bound. Since Unrelated is not
itself an inner class of Outer , there is no implicit enclosing object. Sim-
ilarly, because Unrelated is not a subclass of Outer , the current object of
Unrelated is not a valid enclosing object. We must provide an explicit ref-
erence to an Outer object for the superclass object to bind to. We chose
to supply that reference using an argument to the Unrelated construct-
or, which uses it as an explicit binding reference in the invocation of the
superclass constructor.
Note that you cannot use the inner class creation syntax to externally
provide an Outer object, as in
Outer ref = new Outer();
Unrelated u = ref.new Unrelated(); // INVALID
because this syntax supplies an enclosing object for the Unrelated class
and Unrelated is not an inner class.
An inner class can extend another, unrelated, inner class provided an
appropriate enclosing instance is supplied to the superclass, as just de-
scribed. The resulting inner class then has two enclosing instancesone
for the extended class and one for the superclass. Such designs are con-
voluted, however, and are best avoided.
 
Search WWH ::




Custom Search