> ## 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.

# Kotlin Fun Interface

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.

```kotlin theme={"dark"}
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.

```kotlin theme={"dark"}
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.

```kotlin theme={"dark"}
// 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.

```kotlin theme={"dark"}
// 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.

<div
  style={{ 
display: "flex", 
justifyContent: "space-between", 
alignItems: "center", 
maxWidth: "754px", 
padding: "1rem 0",
marginBottom: "24px"
}}
>
  <span style={{ fontWeight: "bold", fontSize: "1.25rem", color: "var(--tw-prose-headings)", fontFamily: "Inter, ui-sans-serif, system-ui, sans-serif" }}>Tired of Poor Kotlin Skills? Fix That With Deep Grasping!</span>

  <a
    href="https://syntblaze.com"
    target="_blank"
    style={{ 
  marginLeft: "24px",
  textDecoration: "none", 
  backgroundColor: "#007AFF",
  color: "#ffffff", 
  padding: "6px 16px", 
  borderRadius: "16px",
  fontSize: "0.9rem",
  fontWeight: "600",
  textAlign: "center",
  transition: "background-color 0.2s ease"
}}
  >
    Learn More
  </a>
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/skill-tracking.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=b9b0305c93bb501c9e767b5c76c88835" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/skill-tracking.png" />

  <img src="https://mintcdn.com/syntblazellc/23tyuOzaWS88qFlc/images/nuggets.png?fit=max&auto=format&n=23tyuOzaWS88qFlc&q=85&s=c86c80197299762989e9b882419b2109" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/nuggets.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/bite-sized-exercises.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=a65f9a38c37ff28ab73ed783c53c60e3" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/bite-sized-exercises.png" />
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap", marginTop: "12px" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/mastery-chain.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=748a1763454713e679260fbb95f154a2" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/mastery-chain.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-previews.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=242f61448ff5dd6deaaab2dccc13b507" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-previews.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-explanations.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=cf0fc1c31f9cd0fc26716781be05fbc9" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-explanations.png" />
</div>
