August 2017
Intermediate to advanced
440 pages
10h
English
It is really easy to use higher-order functions in Kotlin. The problem is that we often need to interoperate with Java, which natively doesn't support it. It achieves substitution by using interfaces with only one method. This kind of interface is called a Single Abstract Method (SAM) or functional interface. The best example, of situation in which we need to set up a function this way is when we use setOnClickListener on a View element. In Java (until 8), there was no simpler way than by using an anonymous inner class:
//Java
button.setOnClickListener(new OnClickListener() {
@Override public void onClick(View v) {
// Operation
}
});
In the preceding example, the OnClickListener method is the SAM, because it contains ...
Read now
Unlock full access