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.

An interface in Kotlin is a custom type that defines a contract of abstract methods and properties for implementing classes. Unlike abstract classes, interfaces cannot maintain state—they cannot instantiate backing fields—but they can provide default implementations for their functions and property accessors. Kotlin supports multiple interface inheritance, allowing a single class or object to implement multiple interfaces.

Declaration and Implementation

Interfaces are declared using the interface keyword. By default, all members are public and open. A class or object implements an interface using the : operator and must provide implementations for all abstract members using the override modifier.
interface Clickable {
    fun click() // Abstract method, requires implementation
    
    fun hover() { // Concrete method with default implementation
        println("Hovering")
    }
}

class Button : Clickable {
    override fun click() {
        println("Button clicked")
    }
    // hover() is inherited automatically, but can be overridden
}

Interface Properties

Properties declared in an interface can either be abstract or provide implementations for their accessors (get / set). Because interfaces cannot hold state, properties cannot be initialized directly, nor can they utilize a field identifier (backing field).
interface User {
    val id: String // Abstract property, must be overridden in implementing class
    
    val role: String
        get() = "Guest" // Property with custom getter, no backing field
        
    // var count: Int = 0 // ERROR: Property initializers are not allowed in interfaces
}

class StandardUser(override val id: String) : User

Resolving Overriding Conflicts

When a class implements multiple interfaces that contain methods with identical signatures and default implementations, the compiler mandates that the implementing class explicitly override the conflicting method. The super<InterfaceName> syntax is used to invoke specific interface implementations.
interface A {
    fun execute() { println("A") }
}

interface B {
    fun execute() { println("B") }
}

class C : A, B {
    override fun execute() {
        super<A>.execute() // Invokes A's implementation
        super<B>.execute() // Invokes B's implementation
    }
}

Interface Inheritance

Interfaces can inherit from one or multiple other interfaces. A derived interface can declare new members, provide default implementations for inherited abstract members, or override inherited properties. Classes implementing the derived interface are only required to implement the remaining abstract members.
interface Named {
    val name: String
}

interface Person : Named {
    val firstName: String
    val lastName: String
    
    // Overriding the inherited abstract property with a custom getter
    override val name: String 
        get() = "$firstName $lastName"
}

class Employee(
    override val firstName: String,
    override val lastName: String
) : Person // 'name' is already implemented by the Person interface

Functional (SAM) Interfaces

An interface with exactly one abstract method is known as a Functional Interface or a Single Abstract Method (SAM) interface. By declaring it with the fun modifier, Kotlin enables SAM conversion, allowing you to instantiate the interface using lambda expressions.
fun interface IntPredicate {
    fun accept(i: Int): Boolean
}

// Instantiation via SAM conversion (lambda)
val isEven = IntPredicate { it % 2 == 0 }
Master Kotlin with Deep Grasping Methodology!Learn More