182

I am converting some of my Java code to Kotlin and I do not quite understand how to instantiate interfaces that are defined in Kotlin code. As an example, I have an interface (defined in Java code):

public interface MyInterface {
    void onLocationMeasured(Location location);
}

And then further in my Kotlin code I instantiate this interface:

val myObj = new MyInterface { Log.d("...", "...") }

and it works fine. However, when I convert MyInterface to Kotlin:

interface MyInterface {
    fun onLocationMeasured(location: Location)
}

I get an error message: Interface MyListener does not have constructors when I try to instantiate it - though it seems to me that nothing has changed except syntax. Do I misunderstand how interfaces work in Kotlin?

Aleph Aleph
  • 5,215
  • 2
  • 13
  • 28

7 Answers7

281

Your Java code relies on SAM conversion - an automatic conversion of a lambda into an interface with a single abstract method. SAM conversion is currently not supported for interfaces defined in Kotlin. Instead, you need to define an anonymous object implementing the interface:

val obj = object : MyInterface {
    override fun onLocationMeasured(location: Location) { ... }
}
yole
  • 92,896
  • 20
  • 260
  • 197
  • 17
    Many thanks. From the link you posted, I understand that it is preferable to use functional types (e.g. `Location -> Unit`) instead of single-method interfaces if possible - is that correct? – Aleph Aleph May 02 '17 at 12:43
  • 6
    It is correct. You should use functional type wherever possible. – Yoav Sternberg May 02 '17 at 13:32
  • However in my case the interface (SurfaceTextureListener) had multiple methods. – Tash Pemhiwa Nov 14 '18 at 20:01
  • Thanks so much. This answer must have more likes or some special mark, couse it is very useful information and unfortunately some learners can become very confused when learn Kotlin by articles or by "Kotlin in Action" when they look at SAM topic. – TT_W Feb 05 '19 at 13:49
  • 1
    from "Kotlin in Action", yes, you can use lambda in Java's SAM param for shorten and cleaner code, but not Kotlin's SAM, function type is the first class in Kotlin, so, SAM has not meaning for Kotlin, function type with typealias is more Kotlin style. – vg0x00 Jan 13 '20 at 03:40
  • What kind of interface has *constructor* anyway? Isn't the error message itself flawed? – Minh Nghĩa May 16 '21 at 03:39
26

SAM conversion is supported for interfaces defined in Kotlin since 1.4.0

What's New in Kotlin 1.4.0

Before Kotlin 1.4.0, you could apply SAM (Single Abstract Method) conversions only when working with Java methods and Java interfaces from Kotlin. From now on, you can use SAM conversions for Kotlin interfaces as well. To do so, mark a Kotlin interface explicitly as functional with the fun modifier.

SAM conversion applies if you pass a lambda as an argument when an interface with only one single abstract method is expected as a parameter. In this case, the compiler automatically converts the lambda to an instance of the class that implements the abstract member function.

So the example in Your question will look something like this:

fun interface MyInterface {
    fun onLocationMeasured(location: String)
}

fun main() {
    val myObj = MyInterface { println(it) }

    myObj.onLocationMeasured("New York")
}
iknow
  • 8,358
  • 12
  • 41
  • 68
20

The best solution is to use a typealias in-place of your Java interface

typealias MyInterface = (Location) -> Unit

fun addLocationHandler(myInterface:MyInterface) {

}

Register it like this:

val myObject = { location -> ...}
addLocationHandler(myObject)

or even cleaner

addLocationHandler { location -> ...}

Invoke it like this:

myInterface.invoke(location)

The 3 current options seem to be:

  • typealias (messy when called from java)
  • kotlin interface (messy when called from kotlin; you need to create an object) This is a big step back IMO.
  • java interface (less messy when called from kotlin; lambda needs interface name prepended so you don't need an object; also can't use lambda outside of function parenthesis convention)

When converting our libraries to Kotlin, we actually left all the interfaces in Java code, as it was cleaner to call Java from Kotlin than Kotlin from Kotlin.

Angel Koh
  • 12,479
  • 7
  • 64
  • 91
Steven Spungin
  • 27,002
  • 5
  • 88
  • 78
15

Try to access to your interface like this :

 object : MyInterface {
    override fun onSomething() { ... }
}
Jéwôm'
  • 3,753
  • 5
  • 40
  • 73
8

if you have Java class like this :

recyclerView.addOnItemTouchListener(new RecyclerTouchListener(getActivity(), recyclerView, new RecyclerTouchListener.ClickListener()
        {
              //Your Code
        }));

you shoud convert this code from Java to Kotlin like this :

override fun showJozList (list : List<ResponseGetJuzList.Parameter4>) {
        adapter.addData(list)
        jozlist_recycler.addOnItemTouchListener(RecyclerTouchListener(
                activity ,
                jozlist_recycler ,
                object : RecyclerTouchListener.ClickListener
                    {
                          //Your Code
                    }))

convert Java Interface :

new RecyclerTouchListener.ClickListener()

to Kotlin Interface Style:

object : RecyclerTouchListener.ClickListener
Adnan Abdollah Zaki
  • 4,328
  • 6
  • 52
  • 58
3

If the interface is for a listener method of a class, change the interface definition to the function type. That makes the code more concise. See the following.

Class containing listener definition

// A class
private var mLocationMeasuredListener = (location: Location) -> Unit = {}

var setOnLocationMeasuredListener(listener: (location: Location) -> Unit) {
    mLocationMeasuredListener = listener
}

// somewhere in A class
mLocationMeasuredListener(location)

Another class

// B class
aClass.setOnLocationMeasuredListener { location ->
    // your code
}
0
class YourClass : YourInterface {  
    override fun getTest() = "test"    
}

interface YourInterface {
    fun getTest(): String
}

val objectYourClass: YourInterface = YourClass()
print(objectYourClass.getTest())
Braian Coronel
  • 22,105
  • 4
  • 57
  • 62