Sobes.tech
Middle+

How to call the implementation from the interface in an overridden method?

sobes.tech AI

Answer from AI

It is not possible to directly invoke the default implementation of a method from an interface within an overridden method of a class.

You can call the implementation from a superclass if it implements this interface, using super.method().

Example:

interface MyInterface {
    fun doSomething() {
        // Default implementation
        println("Default implementation")
    }
}

open class BaseClass : MyInterface {
    override fun doSomething() {
        // Cannot call MyInterface.super.doSomething()
        println("Base implementation")
    }
}

class DerivedClass : BaseClass() {
    override fun doSomething() {
        // Calls doSomething() from BaseClass
        super.doSomething()
        println("Derived implementation")
    }
}

If you need to access the default implementation from the interface, you can create a helper function in a separate object or class that takes an interface object as a parameter and calls its default implementation. But this is not a direct call from the overridden method.

How to call the implementation from the interface in… - sobes.tech