Skip to main content

Documentation Index

Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt

Use this file to discover all available pages before exploring further.

A functional interface, declared with the fun modifier, is an interface that contains exactly one abstract method. Also known as a Single Abstract Method (SAM) interface, it explicitly opts into SAM conversion. This compiler feature allows the interface to be instantiated using a concise lambda expression whose signature matches the abstract method, bypassing the need for verbose anonymous object expressions. Declaration Syntax To define a functional interface, prepend the standard interface keyword with the fun modifier.
fun interface StringProcessor {
    fun process(input: String): String
}
Structural Constraints The Kotlin compiler enforces strict rules for an interface to qualify as a fun interface:
  1. Single Abstract Method: It must declare exactly one abstract method.
  2. Multiple Implemented Methods: It may contain any number of non-abstract methods (methods with default implementations).
  3. No State: Like standard interfaces, it cannot maintain state (no backing fields).
  4. Subtyping: It can implement other standard interfaces, provided the resulting contract still yields exactly one abstract method.
fun interface ComplexProcessor {
    // The single abstract method (SAM)
    fun execute(data: ByteArray): Boolean

    // Permitted: Non-abstract method
    fun logExecution() {
        println("Execution triggered")
    }
    
    // Permitted: Property with a custom getter (no backing field)
    val processorId: String
        get() = "ID-123"
}
SAM Conversion Mechanics Without the fun modifier, a Kotlin interface must be instantiated using an object expression. When the fun modifier is present, the compiler automatically maps a lambda expression to the single abstract method.
// 1. Traditional instantiation via anonymous object expression
val verboseProcessor = object : StringProcessor {
    override fun process(input: String): String {
        return input.uppercase()
    }
}

// 2. Instantiation via SAM conversion (Lambda expression)
val lambdaProcessor = StringProcessor { input -> 
    input.uppercase() 
}
Type Aliases vs. Fun Interfaces While type aliases can also provide a shorthand for function types, fun interface creates a distinct, nominal type.
// Type alias: Structurally typed (any (Int) -> Boolean matches)
typealias IntPredicateAlias = (Int) -> Boolean

// Fun interface: Nominally typed (requires explicit SAM conversion)
fun interface IntPredicate {
    fun accept(i: Int): Boolean
}
Because fun interface is nominally typed, the compiler guarantees type safety by preventing implicit casting between identical function signatures that belong to different functional interfaces. Java Interoperability Kotlin automatically applies SAM conversion to Java interfaces that contain a single abstract method (e.g., Runnable, Callable). The fun interface construct exists specifically to enable this same lambda-instantiation behavior for interfaces defined natively in Kotlin.
Master Kotlin with Deep Grasping Methodology!Learn More