In java, many libraries (such as the Android SDK), use interfaces which define a Single Abstract Method (a.k.a SAM), for example, in the below interface named Adder, we have a single method called add:
interface Adder {
Integer add(int a, int b);
}
In java, we'd implement it like so:
public class myAdder implements Adder {
public Integer add(int a, int b){
return a+b;
}
}
However in Kotlin, the SAM conversion feature allows us to more do the above more concisely by passing in a lambda whenever an interface has just one method, like so:
val myAdder = Adder{ a,b -> a+b }
//or more concretely...
val runnable = Runnable { println("This runs in a runnable") }
However... SAM conversions can only be used with JAVA interfaces, not kotlin interfaces
In java, many libraries (such as the Android SDK), use interfaces which define a Single Abstract Method (a.k.a SAM), for example, in the below interface named Adder, we have a single method called add:
In java, we'd implement it like so:
However in Kotlin, the SAM conversion feature allows us to more do the above more concisely by passing in a lambda whenever an interface has just one method, like so:
However... SAM conversions can only be used with JAVA interfaces, not kotlin interfaces