Cloud Stack Ninja

When overriding an interface method implemented by class delegation, is it possible to call the class which is normally delegated to from within an overriding function? Similar to how you would call super when using inheritance.

From the documentation:

interface Base {
    fun print()
}

class BaseImpl(val x: Int) : Base {
    override fun print() { print(x) }
}

class Derived(b: Base) : Base by b

fun main(args: Array<String>) {
    val b = BaseImpl(10)
    Derived(b).print() // prints 10
}

Note that overrides work as you might expect: The compiler will use your override implementations instead of those in the delegate object.

override fun print() { ... }

How to call the BaseImpl print() function from within this overridden function?

The use case is I want to add additional logic to this function, while reusing the existing implementation.



Read more here: https://stackoverflow.com/questions/47810973/calling-base-delegated-function-when-using-class-delegation-from-within-overri

Content Attribution

This content was originally published by Steven Jeuris at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: