how do i write in Kotlin like java?
Callback callback= new Callback()
{
@Override
public void getCallback(ServerResponse serverResponse) {
}
}
Android has a mechanism called optional callback that allows your methods and classes to receive the failure and success results of another task without having to call another class time and again just to get results. In this tutorial, we will learn about optional callbacks and how to implement them in Kotlin.
object:Callback() { } is an anonymous class. It has no name when created, before being assigned to var callback . It's similar to the new Callback() code. override replaces @Override. fun indicates that it is a function.
var callback:Callback = object:Callback() {
override fun getCallback(serverResponse:ServerResponse) {
}
}
var callback:Callback
says that the variable type is a Callback
object:Callback() { }
is an anonymous class. It has no name when created, before being assigned to var callback
. It's similar to the new Callback()
code.
override
replaces @Override
fun
indicates that it is a function
You can use following code in Kotlin.
var callback:Callback = object:Callback() {
fun getCallback(serverResponse:ServerResponse) {
}
}
You can use this link to convert your Java code to kotlin. https://try.kotlinlang.org
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With