in , ,

Kotlin Interfaces

Kotlin Interfaces
Kotlin Interfaces

Kotlin Interfaces: In this tutorial, you will learn about interfaces and how to implement it in Kotlin with the assistance of examples.

Interfaces in Kotlin contain the definitions of functions and properties that are abstract.

Kotlin interfaces are like interfaces in Java 8. They can contain meanings of abstract strategies as well as implementations of non-abstract techniques. In any case, they can’t contain any state.

Which means, interface may have property but it should be abstract or needs to provide accessor executions.


Recommended Reading: Kotlin Abstract Class

Abstract classes in Kotlin are like interface with one significant difference. It’s not compulsory for properties of an abstract class to be abstract or provide accessor executions.


How to characterize an interface?

Catchphrase interface is used to characterize interfaces in Kotlin. For instance,

interface MyInterface {

    var test: String   // abstract property

    fun foo()          // abstract method
    fun hello() = "Hello there" // method with default implementation
}

Here,

  • an interface MyInterface is created.
  • the interface has an abstract property test and an abstract method foo().
  • the interface also has a non-abstract method hello().

How to execute interface?

Here’s the way a class or object can execute the interface:

interface MyInterface {

    val test: Int   // abstract property

    fun foo() : String   // abstract method (returns String)
    fun hello() {   // method with default implementation
        // body (optional)
    }
}

class InterfaceImp : MyInterface {

    override val test: Int = 25
    override fun foo() = "Lol"

    // other code
}

Here, a class InterfaceImp implements the MyInterface interface.

The class overrides abstract individuals (test property and foo() strategy) for the interface.


Example: How interface works?

interface MyInterface {

    val test: Int

    fun foo() : String

    fun hello() {
        println("Hello there, pal!")
    }
}

class InterfaceImp : MyInterface {

    override val test: Int = 25
    override fun foo() = "Lol"

}

fun main(args: Array<String>) {
    val obj = InterfaceImp()

    println("test = ${obj.test}")
    print("Calling hello(): ")

    obj.hello()

    print("Calling and printing foo(): ")
    println(obj.foo())
}

At the point when you run the program, the output will be:

test = 25
Calling hello(): Hello there, pal!
Calling and printing foo(): Lol

As referenced over, an interface may likewise have a property that provide accessor execution. For instance,

interface MyInterface {

    // property with implementation
    val prop: Int
        get() = 23
}

class InterfaceImp : MyInterface {
    // class body
}

fun main(args: Array<String>) {
    val obj = InterfaceImp()

    println(obj.prop)
}

At the point when you run the program, the output will be:

23

Here, prop isn’t abstract. Be that as it may, it’s valid inside the interface since it provides execution to accessor.

In any case, you can’t accomplish something like val prop: Int = 23 inside the interface.


Implementing Two or More Interfaces in a Class

Kotlin doesn’t allow genuine multiple inheritances. Be that as it may, it’s feasible to implement two or more interfaces in a single class. For instance,

interface A {

    fun callMe() {
        println("From interface A")
    }
}

interface B  {
    fun callMeToo() {
        println("From interface B")
    }
}

// implements two interfaces A and B
class Child: A, B

fun main(args: Array<String>) {
    val obj = Child()

    obj.callMe()
    obj.callMeToo()
}

At the point when you run the program, the output will be:

From interface A
From interface B

Resolving overriding conflicts (Multiple Interface)

Assume, two interfaces(A and B) have a non-abstract strategy with a similar name (suppose callMe() technique). You implemented these two interfaces in a class (suppose C). Presently, on the off chance that you call the callMe() strategy using the object of class C, compiler will throw blunder. For instance,

interface A {

    fun callMe() {
        println("From interface A")
    }
}

interface B  {
    fun callMe() {
        println("From interface B")
    }
}

class Child: A, B 

fun main(args: Array<String>) {
    val obj = Child()

    obj.callMe()
}

Here’s the error:

Error:(14, 1) Kotlin: Class 'C' must override public open fun callMe(): Unit defined in A because it inherits multiple interface methods of it

To tackle this issue, you need to provide your own execution. Here’s how:

interface A {

    fun callMe() {
        println("From interface A")
    }
}

interface B  {
    fun callMe() {
        println("From interface B")
    }
}

class C: A, B {
    override fun callMe() {
        super<A>.callMe()
        super<B>.callMe()
    }
}

fun main(args: Array<String>) {
    val obj = C()

    obj.callMe()
}
From interface A
From interface B

Here, explicit execution of callMe() strategy is provided in class C.

class C: A, B {
    override fun callMe() {
        super<A>.callMe()
        super<B>.callMe()
    }
}

The statement super.callMe() calls the callMe() method of class A. Similarly, super.callMe() calls the callMe() method of class B.


Thanks for reading! We hope you found this tutorial helpful and we would love to hear your feedback in the Comments section below. And show us what you’ve learned by sharing your photos and creative projects with us.

salman khan

Written by worldofitech

Leave a Reply

Kotlin Abstract Class

Kotlin Abstract Class

Kotlin Nested and Inner Class

Kotlin Nested and Inner Class