Kotlin Interfaces

Kotlin Interfaces : Kotlin Interfaces are similar to Kotlin Classes, and can contain uninitialized variables, abstract and non-abstract methods.

If a class is implementing an interface, the class is making a promise that the abstract methods in the interface shall be implemented. Compiler makes sure that this promise is not broken and if it does, compiler throws an error and stops.

Kotlin Interface Example

The following program is an example for basic Kotlin Interface with variables, abstract methods, and non-abstract methods.

Kotlin Program – example.kt

</>
Copy
interface Vehicle {
    // uninitialized variables
    var name : String
    var medium : String

    // non-abstract method
    fun runsWhere() {
        println("The vehicle, $name, runs in $medium")
    }

    // abstract method
    fun howItRuns()
}
  • interface keyword is used to define an interface.
  • Kotlin Interface cannot have initialized variables.
  • Kotlin Interface can have non-abstract and abstract methods.

Implement an Interface

Kotlin Classes can implement an interface and these classes should override the abstract methods and variables of Kotlin Interface. Following is an example where Aeroplane class implements Vehicle interface.

Kotlin Program – example.kt

</>
Copy
/**
 * Interface
 */
interface Vehicle {
    // uninitialized variables
    var name : String
    var medium : String

    // non-abstract method
    fun runsWhere() {
        println("The vehicle, $name, runs in $medium")
    }

    // abstract method
    fun howItRuns()
}

/**
 * implementing interface
 */
class Aeroplane : Vehicle {
    // override abstract variables and functions of interface
    override var name:String = "Aeroplane"
    override var medium: String = "air"

    override fun howItRuns() {
        println("$name flies based on buoyancy force.")
    }
}

fun main(args: Array<String>) {
    var vehicle1 = Aeroplane()
    vehicle1.runsWhere()
    vehicle1.howItRuns()
}

Output

The vehicle, Aeroplane, runs in air
Aeroplane flies based on buoyancy force.

Implement multiple interfaces

A Kotlin Class can implement multiple Kotlin Interfaces. All the abstract methods and variables of the interfaces being implemented must be overridden in the class.

In the following example, Aeroplane class implement two interfaces:Vehicle and ArielVehicle. So, Aeroplane has to implement all the abstract members of the interfaces Vehicle and ArielVehicle.

Kotlin Program – example.kt

</>
Copy
/**
 * Interface
 */
interface Vehicle {
    // uninitialized variables
    var name : String
    var medium : String

    // non-abstract method
    fun runsWhere() {
        println("The vehicle, $name, runs in $medium")
    }

    // abstract method
    fun howItRuns()
}

/**
 * Another Interface
 */
interface ArielVehicle {
    // uninitialized variables
    var maximum_altitude:Int // in feet
}

/**
 * implementing interface
 */
class Aeroplane : Vehicle, ArielVehicle {
    // override abstract variables and functions of interface
    override var name:String = "Aeroplane"
    override var medium: String = "air"
    override var maximum_altitude: Int = 10000

    override fun howItRuns() {
        println("$name flies based on buoyancy force.")
    }
}

Conclusion

In this Kotlin Tutorial, we have learnt about Kotlin Interfaces and how to implement them. In our next tutorial, we shall learn Kotlin Abstract Classes.