How to call superclass of super class from inner class in Kotlin?

What is the Kotlin Java equivalent OuterClass.super.method()?

Example (in Java):

class Outer {
    class Inner {
        void someMethod() {
            Outer.super.someOtherMethod();
        }
    }

    @Override
    public String someOtherMethod() {
        // This is not called...
    }
}
+4
source share
2 answers

Use the syntax super@OuterClass.method():

open class C {
    open fun f() { println("C.f()") }
}

class D : C() {
    override fun f() { println("D.f()") }

    inner class X {
        fun g() {
            super@D.f() // <- here
        }
    }
}

This is similar to how Java OuterClass.this expresses itself in Kotlin asthis@OuterClass .

+7
source

This would be equivalent in Kotlin:

internal class Outer {
    internal inner class Inner {
        fun myMethod() {
            println(super@Outer.toString())
        }
    }

    override fun toString(): String {
        return "Blah"
    }
}
0
source

Source: https://habr.com/ru/post/1682496/


All Articles