142

I have a third party Java library which an object with interface like this:

public interface Handler<C> {
  void call(C context) throws Exception;
}

How can I concisely implement it in Kotlin similar to Java anonymous class like this:

Handler<MyContext> handler = new Handler<MyContext> {
   @Override
   public void call(MyContext context) throws Exception {
      System.out.println("Hello world");
   }
}

handler.call(myContext) // Prints "Hello world"
Peter Lamberg
  • 8,151
  • 3
  • 55
  • 69

5 Answers5

199

Assuming the interface has only a single method you can make use of SAM.

val handler = Handler<String> { println("Hello: $it") }

Since version 1.4 Kotlin supports SAM for interfaces defined in Kotlin. That requires prefixing the interface keyword with fun

fun interface Handler<C> {
  fun call(context: C);
}

If you have a method that accepts a handler then you can even omit type arguments:

fun acceptHandler(handler:Handler<String>){}

acceptHandler(Handler { println("Hello: $it") })

acceptHandler({ println("Hello: $it") })

acceptHandler { println("Hello: $it") }

If the interface has more than one method the syntax is a bit more verbose:

val handler = object: Handler2<String> {
    override fun call(context: String?) { println("Call: $context") }
    override fun run(context: String?) { println("Run: $context")  }
}
miensol
  • 39,733
  • 7
  • 116
  • 112
  • 2
    `acceptHandler { println("Hello: $it")}` would also work in most cases – voddan Jun 07 '16 at 23:11
  • 6
    For anyone struggling . i think the interface must be declared in java. i think SAM conversion not working for kotlin interfaces. if its a kotlin interface you have to use the object:Handler{} way. per here: https://youtrack.jetbrains.com/issue/KT-7770 . – j2emanue Dec 31 '18 at 05:03
  • 6
    You can do this with a Kotlin interface as of 1.4 - you just declare it as a `fun interface`. – Nick Sep 04 '20 at 14:40
  • @Nick I'd recommend adding that as a separate answer, or editing it into this answer – Simon Forsberg Apr 04 '21 at 19:25
38

I had a case where I did not want to create a var for it but do it inline. The way I achieved it is

funA(object: InterfaceListener {
                        override fun OnMethod1() {}

                        override fun OnMethod2() {}
})
Aalap
  • 2,847
  • 2
  • 26
  • 24
31
     val obj = object : MyInterface {
         override fun function1(arg:Int) { ... }

         override fun function12(arg:Int,arg:Int) { ... }
     }
kk.
  • 3,747
  • 12
  • 36
  • 67
pruthwiraj.kadam
  • 1,033
  • 10
  • 11
3

As of Kotlin 1.4 you can declare a functional interface:

fun interface Handler<C> {
  fun call(context: C);
}

and then you can create one concisely:

val handler = Handler<String> {
  println("Handling $it")
}

Demo

Nick
  • 11,475
  • 1
  • 36
  • 47
2

The simplest answer probably is the Kotlin's lambda:

val handler = Handler<MyContext> {
  println("Hello world")
}

handler.call(myContext) // Prints "Hello world"
Peter Lamberg
  • 8,151
  • 3
  • 55
  • 69