Can you extend an inner class in Kotlin, without extending it's outer class?

330 Views Asked by At

In Java you can do this:

class Outer {
    class Inner {
    }
}

class ExtendedClass extends Outer.Inner{
    ExtendedClass(Outer outer) {
        outer.super();
    }
}

So I'd expect in Kotlin you write something like this:

class ExtendedClass(outer: Outer): Outer.Inner(outer: Outer) {
}

Unfortunately this doesn't work.

I know you can do this:

class ExtendedOuter: Outer() {
    inner class ExtendedInner: Inner(){
    }
}

but this forces you to extend the Outer Class which is not always desirable.

This brings me to the question:

Can you extend an inner class in Kotlin, without extending it's outer class? If not, why are we allowed to declare non-open outer classes, when they contain an open inner class?

0

There are 0 best solutions below