How can I do 'qualified superclass constructor invocation' in Kotlin?

I’m looking for a way to extend a non-static nested class from Kotlin.

From the JLS Chapter 8
Example 8.8.7.1-1. Qualified Superclass Constructor Invocation

// Java Code
class Outer {
    class Inner {}
}

class ChildOfInner extends Outer.Inner {
    ChildOfInner() { (new Outer()).super(); }
}

I tried this:

// Kotlin Code
class Outer {
	open inner class Inner {}
}

val outer = Outer()

class ChildOfInner(): outer.Inner()

Is there any way to do this in Kotlin?

I’m not sure there’s a way, as a workaround you can make Inner static in Java (not inner in Kotlin) and explicitly pass it an instance of Outer.

1 Like

Thanks for the answer.

class Outer {
   open class Inner(val outer: Outer) {}
}

val outer = Outer()

class ChildOfInner(): Inner(outer)

Seems like a good idea. :slight_smile:
I would like to use it if there is really no way to solve this problem.