michal michal - 9 months ago 42
Java Question

How this Inner class inheritance example works?

I was reading about inheritance from inner classes and I got stuck trying to understand how the code below works:

class Outer{
class Inner{
class Derived extends Outer.Inner{
Derived(Outer o){

As far as I know, the keyword super refers to the superclass, super() to it's no argument constructor. In case of Outer class, the superclass is Object class.
I would like to understand, how this code works.
Why instantiation of Object class makes extending Inner class possible?
What creating an Object class instance has to do with creating Outer class object?

Removing line containing "o.super();" cause error:
java: an enclosing instance that contains p.Outer.Inner is required

I read many articles but I can't figure out how it works.

I will be greatful for any answer.


Suppose you have a variable o referring to an instance of Outer, and you want to create an instance of Inner with its enclosing instance being the value of o. You could call:

Outer.Inner inner = o.new Outer.Inner();

It's rarely seen in Java (because usually when you construct an inner class, you do it with "this" being the implicit enclosing instance), but it's valid.

This is really the equivalent of that, but in the context of a subclass constructor. The Inner class really has a constructor like this:

Inner(Outer enclosingInstance)

... and this line is effectively calling


... it's just it's not expressed that way in syntax to differentiate between this "special" information and any regular parameters.