paint-brush
The Interconnection of Functional Interfaces in Java and Kotlinby@amrdeveloper
205 reads

The Interconnection of Functional Interfaces in Java and Kotlin

by Amr HeshamMarch 14th, 2022
Read on Terminal Reader
Read this story w/o Javascript
tldt arrow

Too Long; Didn't Read

SAM stands for Single Abstract Method interface and it’s called also Functional interface. Functional interface is an interface with only one non-default method and any number of default methods. In Kotlin you can use Java or Kotlin Functional interfaces as a Lambda expression for example. The same syntax will work on Kotlin also if your functional interface is written in Java like OnClickListener. The syntax is not an extension but SAM conversion, but what does SAM stand for? You can use the same syntax to call your custom listener like setOnClick listener.

Company Mentioned

Mention Thumbnail
featured image - The Interconnection of Functional Interfaces in Java and Kotlin
Amr Hesham HackerNoon profile picture


Hello everyone, almost all of us have seen code like view.setOnClickListener { } or view.setOnLongClickListener { } in Kotlin, and when you click on it to see the source code it will show you the Java OnClickListener interface. But have you asked yourself what is this syntax? Is it a Kotlin extension? and how can you call your custom listener like this? In this small article, you will know the answer to all of those questions, so let's start 😋.


This syntax is not an extension but SAM conversion, but what does SAM stand for?


SAM stands for Single Abstract Method interface and it’s called also Functional interface which is an interface with only one non-default method and any number of default methods, for examples In the JDK we have Runnable class which has only one method called run and in Android SDK we have OnClickListener, OnLongClickListener …etc.


How Can We Create a Custom Functional Interface?

To declare a Functional interface in Java is easy. You just create a normal interface with one method for example.


public interface FunInterface {
    void method();
}


And in Kotlin starting from Version 1.4 you need to declare it as a fun interface not only interface for example.


fun interface FunInterface {
    fun method()
}


In Kotlin you can use Java or Kotlin Functional interfaces as a Lambda expression for example.


fun learnFunInterface(fi : FunInterface) { ... }


To you this function you can pass FunInterface as an anonymous object for example.


learnFunInterface(object : FunInterface { 
    override fun method() {
        println("Hello") 
    } 
})


But because it Functional Interface you can pass it as lambda { } and this is what is called SAM conversion.


learnFunInterface ({ println("Hello") })


also in Kotlin if your last parameter is a functional interface you can move your lambda out the brackets ( ) for example.


learnFunInterface {
    println("Hello")
}


Now you can see that setOnClickListener { } is just because this method takes functional interface which is the OnClickListener as a parameter.


Note that the same syntax will work on Kotlin also if your functional interface is written in Java like OnClickListener.


Extra Resources:

You can find me on: GitHubLinkedInTwitter.


Enjoy Programming 😋.


This article was first published here.