Search code examples
kotlininheritanceinterfaceoverridingabstract

"Can't access abstract member directly." How do I overcome this aspect of inheriting an interface?


class meow(): InputConnection{
     override fun getTextBeforeCursor(before:Int,after:Int):CharSequence?{
       return super.getTextBeforeCursor(before,after)
     }


}

but whai does inheriting an interface both require overriding, and say `cannot access this directly'? what the above errors

This image is the errors from this. One for there being more stuff in need of override (yes) and another how this override can't access abstract member directly (why?).

And not just 'why', but hao inherit an interface with this aspect correctly?


Solution

  • You cannot call an abstract function because it has no definition of what to do when called.

    If an interface declares a function without any default implementation, then it is abstract.

    In this case, evidently the InputConnection class does not define a default implementation for this function. InputConnection.getTextBeforeCursor() is abstract, so you cannot call it using super.getTextBeforeCursor().

    To do this correctly, it is up to you to define the content of this function and how it will behave without any reliance on a super version of this function.