Alexpl292
07/11/2019, 11:10 AMtseisel
07/11/2019, 12:30 PMprotected
in the body of the Java class :
public abstract class BaseJava {
protected void foo(String str, int bar) {
// Implementation of your function.
// Notice the signature : its first parameter is a String, which is the expected receiver of your extension function in Kotlin.
}
}
In Kotlin, you could define this extension in each derived class :
class Derived : BaseJava() {
private fun String.foo(bar: Int): Unit = foo(this, bar)
fun myFun() {
"Hello World!".foo(42)
}
}
But that sounds like a lot of efforts just to have a nicer syntax for some function calls. I'd stick with calling the protected
method by passing the String
parameter.