Reputation: 8507
Super constructor invocation definition:
[Primary.] [NonWildTypeArguments] super ( ArgumentListopt ) ;
A super constructor call can be prefixed by an Primary
expression. Example (taken from JLS):
class Outer {
class Inner{ }
}
class ChildOfInner extends Outer.Inner {
ChildOfInner() {
(new Outer()).super(); // (new Outer()) is the Primary
}
}
Does a Primary
expression exist that makes the call to super() the invocation of a constructor of the calling class? Or Java prevents that?
Upvotes: 3
Views: 610
Reputation: 87573
Does a Primary expression exist that makes the call to super() the invocation of a constructor of the calling class?
No. There is no way to divert the super()
call be the equivalent of using this()
. The sole reason for Primary is to provide the instantiating outer context when the superclass is a non-static inner class.
The Primary expression is not selecting a constructor or class - its just passing context information required by some constructors (i.e. the outer instance required by an non-static inner class).
Edit:
The OP asks for a source. I don't have one - I basing this answer solely on my reading of the spec, which I think is pretty clear:
... the keyword super ... used to invoke a constructor of the direct superclass.
If the superclass constructor invocation is qualified, then the Primary expression p immediately preceding ".super" is ... the immediately enclosing [outer] instance ... it is a compile-time error if the type of p is not O [outer class] or a subclass of O.
If there's a qualifying expression on super
, it can only be used to return an instance of the enclosing class of the superclass (or an instance of a subclass of the enclosing class of the superclass). The qualifying expression has no bearing on which class' constructor is called in response to super()
, i.e. there is no value that the qualifying expression can return that will change which class whose constructor is to be invoked as super()
- super()
will always invoke a constructor of the direct superclass.
Upvotes: 2
Reputation: 12243
From what i know the constructors in Java aren't virtual (a base class can't call any constructor from the derived class). You can actually invoke methods inside the constructor and these will be called as they would on the finalized object. But this has the potential to introduce some subtle bugs (because there is no initialization performed on the derived class members when that method is invoked from a base class constructor).
Check this for a more complete discussion:
http://www.codeguru.com/java/tij/tij0082.shtml
Upvotes: 0