When we create an inner class, an object of that inner class contains an implicit link to the enclosing object that made it. The “secret” reference to the enclosing class object must be initialized, and yet in the derived class there’s no longer a default object to attach to. We must use a special syntax to make the association explicit:
// innerclasses/InheritInner.java
// (c)2017 MindView LLC: see Copyright.txt
// We make no guarantees that this code is fit for any purpose.
// Visit http://OnJava8.com for more book information.
// Inheriting an inner class
class WithInner {
class Inner {}
}
public class InheritInner extends WithInner.Inner {
// - InheritInner() {} // Won't compile [1]
InheritInner(WithInner wi) {
wi.super();
}
public static void main(String[] args) {
WithInner wi = new WithInner();
InheritInner ii = new InheritInner(wi);
}
}
detail error at [1]:
innerclasses/InheritInner.java:12: error: an enclosing instance that contains W
ithInner.Inner is required
InheritInner() {} // Won't compile
^
1 error
This solution is wi.super().
references:
1. On Java 8 - Bruce Eckel
2. https://github.com/wangbingfeng/OnJava8-Examples/blob/master/innerclasses/InheritInner.java