Kotlin Interface

In this tutorial we are going to talk about Interfaces in Kotlin. Interfaces are almost like abstract classes with few differences.

What is Interface?

Interfaces in Kotlin can contain both abstract method declarations and method implementations.
Interfaces differ from abstract classes in that they cannot store state. They are allowed to have properties, but they must be abstract or provide accessor implementations.

Interfaces are declared using the interface keyword.

Creating Interface

interface MyInterface
{
     fun overrideMe()
     fun iAmHereOnly()
     {
           // optional body
     }

}

Implementing Interface

A class can implement one or more interfaces.

class MyClass : MyInterface
{
    override fun play()
    {
        println("MyClass.play()")
    }
}

Interface Example

// www.raviroza.com
// 04-Feb-2022, 6.20 pm
interface Vehicle
{
    fun start()
    fun defaultMethod()
    {
        println("Vehicle.Default Method")
    }
}
class Bike : Vehicle
{
    override fun start() {
        println("Bike starts")
    }
}
class Car : Vehicle
{
    override fun start() {
        println("Car starts")
    }
}

fun main()
{
    println("interface demo")
    println("-----------------")
    // object of type Bike
    var honda = Bike()
    honda.start()


    // object of type Car
    var hyundai = Car()
    hyundai.start()


    println("\nusing interface ref")
    println("-----------------")
    // using interface ref to instantiate car
    var vehicle : Vehicle = Bike()
    vehicle.start()

    // using the same ref to instantiate bike
    vehicle = Car()
    vehicle.start()
}
/*
OUTPUT
interface demo
-----------------
Bike starts
Car starts

using interface ref
-----------------
Bike starts
Car starts
 */

Invoking Super Behavior

Like Java, in Kotlin the super keyword is used to call the function of its supertype (class or interface). To invoke a function on a supertype, we will need three things: (1) the super keyword; (2) name of the supertype enclosed in a pair of angle brackets; and (3) the name of function we want to invoke on the supertype. It looks something like the code snippet here:

super<SuperTypeName>.functionName()

Below is an example for the same:

Multiple inheritance using interface

// www.raviroza.com
// 04-Feb-2022, 6.37 pm
interface Interface1
{
    fun download ()
    fun status() = println ("download in progress")
}
interface Interface2
{
    fun upload()
    fun status() = println ("upload in progress")
}

class MyTelegram : Interface1, Interface2
{
    override fun download() 
    {
        println ("download starts")
    }

    override fun upload() 
    {
        println ("upload starts")
    }

    override fun status() {
        super<Interface1>.status()
        super<Interface2>.status()
    }
}
fun main()
{
    println("Multiple inheritance using interface")
    println("-------------------------------------")
    var t = MyTelegram()
    t.download()
    t.upload()
    t.status()
}

/*
OUTPUT:
Multiple inheritance using interface
-------------------------------------
download starts
upload starts
download in progress
upload in progress
 */