Reputation: 1095
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();}
}
Upvotes: 0
Views: 232
Reputation: 763
I'm not sure I understand what foo1.this();
is supposed to do. Perhaps you are trying to call the default constructor of Inner
. Since Inner
is 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 a Foo1
argument containing the reference to its parent object.
The call to foo1.super()
, however, will work just fine. It will call the constructor of the new Inner
instance with foo1
as implicit parent reference. That is, the new Inner
instance will be tied to the given Foo1
instance.
As others have pointed out, you can of cause make your Inner
class static, in which case it does not contain an implicit reference to a Foo1
instance. Then you can simply call super();
in your Foo2
constructor.
Upvotes: 1
Reputation: 11909
If you declare the Inner class static you can do it else you need an instance of the outer class Foo1.
class Foo1 {
static class Inner {
}
}
But your direct questions seems strange:
foo1.this(); //won't compile
What are you trying to do here?
Call a method called "this" it looks like, but hoping to use the no-argument constructor perhaps? Just use this()
if so.
Upvotes: 2
Reputation: 15363
Make your inner class static
. There is no instance from which to get a non static class definition
Upvotes: 0