How to initialize a class inheriting from an inner class? Why foo1.this() won’t compile?
class Foo1 {
class Inner {
}
}
class Foo2 extends Foo1.Inner {
//Foo2(Foo1 foo1) {foo1.this();} //won't compile
Foo2(Foo1 foo1) {foo1.super();}
}
I’m not sure I understand what
foo1.this();is supposed to do. Perhaps you are trying to call the default constructor ofInner. SinceInneris a not a static class, it does not have a no argument constructor. It only looks that way. In reality, it has a constructor which takes aFoo1argument containing the reference to its parent object.The call to
foo1.super(), however, will work just fine. It will call the constructor of the newInnerinstance withfoo1as implicit parent reference. That is, the newInnerinstance will be tied to the givenFoo1instance.As others have pointed out, you can of cause make your
Innerclass static, in which case it does not contain an implicit reference to aFoo1instance. Then you can simply callsuper();in yourFoo2constructor.