Easy Tutorial
❮ Kotlin Setup Kotlin Loop Control ❯

Kotlin Interfaces

Kotlin interfaces are similar to Java 8, using the interface keyword to define interfaces, allowing methods to have default implementations:

interface MyInterface {
    fun bar()    // not implemented
    fun foo() {  // implemented
      // optional method body
      println("foo")
    }
}

Implementing Interfaces

A class or object can implement one or more interfaces.

class Child : MyInterface {
    override fun bar() {
        // method body
    }
}

Example

interface MyInterface {
    fun bar()
    fun foo() {
        // optional method body
        println("foo")
    }
}
class Child : MyInterface {
    override fun bar() {
        // method body
        println("bar")
    }
}
fun main(args: Array<String>) {
    val c =  Child()
    c.foo();
    c.bar();

}

Output result:

foo
bar

Properties in Interfaces

Properties in interfaces can only be abstract, no initialization values are allowed, interfaces do not store property values, and properties must be overridden when implementing the interface:

interface MyInterface{
    var name:String //name property, abstract
}

class MyImpl:MyInterface{
    override var name: String = "tutorialpro" //override property
}

Example

interface MyInterface {
    var name:String //name property, abstract
    fun bar()
    fun foo() {
        // optional method body
        println("foo")
    }
}
class Child : MyInterface {
    override var name: String = "tutorialpro" //override property
    override fun bar() {
        // method body
        println("bar")
    }
}
fun main(args: Array<String>) {
    val c =  Child()
    c.foo();
    c.bar();
    println(c.name)

}

Output result:

foo
bar
tutorialpro

Function Overriding

When implementing multiple interfaces, you may encounter the problem of inheriting multiple implementations of the same method. For example:

Example

interface A {
    fun foo() { print("A") }   // implemented
    fun bar()                  // not implemented, no method body, is abstract
}

interface B {
    fun foo() { print("B") }   // implemented
    fun bar() { print("bar") } // implemented
}

class C : A {
    override fun bar() { print("bar") }   // override
}

class D : A, B {
    override fun foo() {
        super<A>.foo()
        super<B>.foo()
    }

    override fun bar() {
        super<B>.bar()
    }
}

fun main(args: Array<String>) {
    val d =  D()
    d.foo();
    d.bar();
}

Output result:

ABbar

In the example, interfaces A and B both define the methods foo() and bar(). Both implement foo(), and B implements bar(). Since C is a concrete class that implements A, it must override and implement the abstract method bar().

However, if we derive D from A and B, we need to implement all the methods inherited from multiple interfaces and specify how D should implement them. This rule applies both to methods inherited with a single implementation (bar()) and to methods inherited with multiple implementations (foo()).

❮ Kotlin Setup Kotlin Loop Control ❯