ProAndroidDev

The latest posts from Android Professionals and Google Developer Experts.

Follow publication

Everything you want to know about Functional interfaces in Kotlin

Sandeep Kella
ProAndroidDev
Published in
3 min readMay 26, 2024

Functional interfaces in Kotlin are a key feature that enhances its functional programming capabilities and ensures smooth interoperability with Java. Here’s a comprehensive overview of functional interfaces in Kotlin:

Definition and Characteristics

  1. Single Abstract Method (SAM):
  • A functional interface must have exactly one abstract method. This method represents the interface’s single behavior.
  • The interface can have multiple default or static methods, but only one method can be abstract.

2. @FunctionalInterface Annotation:

  • This annotation is not mandatory but helps indicate that an interface is intended to be a functional interface.
  • It provides compile-time checking to ensure the interface adheres to the single abstract method constraint.
@FunctionalInterface
interface MyFunctionalInterface {
fun performAction(name: String)
}

3. Interoperability with Java:

  • Kotlin’s functional interfaces are fully compatible with Java’s functional interfaces.
  • You can use Kotlin lambdas with Java APIs that expect functional interfaces, such as Runnable, Callable, and Comparator.

SAM Conversion

Kotlin supports SAM conversion, allowing you to pass lambda expressions where a functional interface is expected. This feature simplifies the syntax and usage of functional interfaces.

Example of SAM Conversion

fun runAction(action: MyFunctionalInterface) {
action.performAction("SAM Conversion")
}

fun main() {
// Passing a lambda directly to a function expecting a functional interface
runAction { name ->
println("Running action with $name")
}
}

In this example, runAction expects a parameter of type MyFunctionalInterface. Instead of explicitly creating an instance of the interface, we pass a lambda expression directly, and Kotlin automatically converts it to the functional interface instance.

Creating and Using Functional Interfaces

Defining a Functional Interface

@FunctionalInterface
interface Greeter {
fun greet(name: String)
}

Implementing and Using a Functional Interface

fun main() {
// Using a lambda expression to implement the functional interface
val greeter: Greeter = Greeter { name ->
println("Hello, $name!")
}

// Calling the method defined in the functional interface
greeter.greet("Kotlin")
}

In this example, Greeter is a functional interface with a single abstract method greet. We implement this interface using a lambda expression and call the greet method.

Functional Interfaces with Default Methods

Functional interfaces can have default methods, which provide additional functionality without breaking the single abstract method rule.

@FunctionalInterface
interface Worker {
fun work(task: String)

// Default method
fun rest() {
println("Taking a break")
}
}

fun main() {
val worker: Worker = Worker { task ->
println("Working on $task")
}

worker.work("Project")
worker.rest() // Calling the default method
}

Higher-Order Functions and Functional Interfaces

Higher-order functions in Kotlin can accept functional interfaces as parameters, enabling more flexible and reusable code.

Example with Higher-Order Functions

fun executeTask(worker: Worker, task: String) {
worker.work(task)
}

fun main() {
val worker: Worker = Worker { task ->
println("Completing $task")
}

executeTask(worker, "assignment")
}

In this example, executeTask is a higher-order function that takes a Worker functional interface as a parameter. We pass a lambda expression that is converted to the Worker interface.

Interoperability with Java

Since Kotlin is fully interoperable with Java, you can use Java’s functional interfaces directly in Kotlin code.

Example Using Java’s Runnable Interface

fun main() {
val runnable = Runnable {
println("Running from a Kotlin lambda")
}

val thread = Thread(runnable)
thread.start()
}

Here, we use Java’s Runnable interface, demonstrating how seamlessly Kotlin integrates with Java’s functional programming constructs.

Summary

Functional interfaces in Kotlin are interfaces with a single abstract method, designed to facilitate functional programming and enhance Java interoperability. Key features include:

  • Single Abstract Method (SAM): Ensures the interface has only one abstract method.
  • @FunctionalInterface Annotation: Optional annotation to enforce SAM compliance.
  • SAM Conversion: Allows lambdas to be used where functional interfaces are expected.
  • Interoperability with Java: Enables seamless use of Java functional interfaces in Kotlin.
  • Default Methods: Provides additional functionality without breaking the single abstract method constraint.
  • Higher-Order Functions: Supports passing functional interfaces as parameters for flexible and reusable code.

By leveraging functional interfaces, Kotlin developers can write cleaner, more expressive, and interoperable code.

Published in ProAndroidDev

The latest posts from Android Professionals and Google Developer Experts.

Written by Sandeep Kella

Android dev by day, tech writer by night. Breaking down code so even my cat could understand it. Join me on Medium for fun, geeky, and practical Android tips!

Responses (2)

Write a response