I am befuddled why this is allowed
public class Foo {
class Bar extends Foo {
}
}
Yet this is not allowed
public class Foo {
class Bar extends Foo {
}
class Fooey extends Bar {
}
}
The compiler informed that it can not reference Fooey.this before supertype constructor has been called.
And this is allowed
public class Foo {
static class Bar extends Foo {
}
class Fooey extends Bar {
}
}
What is going on here? And where can I go to find more information on how inner class inheritance works?
EDIT I came upon both rather poor ideas; inner class extends outer class and inner class extends other static inner class. I wasn’t sure what exactly was going and how I should refactor this. I ended up just yanking out the inner classes and encapsulating them in the outer class.
First of all: Don’t do this sort of thing. It’s evil. Really, Java 1.1 should have been specified very much more restrictively, IMO.
There is confusion about which
thisto use from theFoo.Fooeyconstructor. The outer this (Foo.this) would work. But the actualthisis aFoobut it can’t be passed to the superconstructor because of rules about usingthisbefore the superconstructor returns (and besides having an outer instance the same instance as the inner instance is fecked up). The outer this on the superclass “((Bar)this).this$0” (IIRC), is also inaccessible due to restrictions on use ofthis.The solution is to be explicit. Explicit is usually a good thing in my book (unless it becomes boilerplate).
Better yet, don’t have an inner class extend its own outer class, or extend any inner class.