How to write following code in Kotlin for callback implementation

10,469

Solution 1

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

Solution 2

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

Share:
10,469
Kamlakar Kate
Author by

Kamlakar Kate

Updated on August 01, 2022

Comments

  • Kamlakar Kate
    Kamlakar Kate over 1 year

    how do i write in Kotlin like java?


    Callback callback= new Callback()
     {
         @Override
         public void getCallback(ServerResponse serverResponse) {
    
         }
     }