In this tutorial, we’ll be looking into interfaces in Kotlin. Kotlin interface is like contracts that can be used by classes for specific behaviors.
Kotlin Interface
Interfaces in Kotlin contain the definitions of functions and properties that are abstract.
Interfaces can have the implementation of the functions.
Also, interfaces can have non-abstract properties by defining their getters and setters.
But still, an interface can’t store the state. Interfaces are useless unless they’re implemented by a class.
An interface can’t have any constructor.
Defining an Interface
An interface in Kotlin can be defined using the keyword interface
.
1 2 3 4 5 6 7 8 9 10 |
interface First{ fun stringEcho() : String //abstract function that returns a string fun printMe(){ println("This function has a body containing the implementation") } var str : String //abstract property val i : Int //abstract property } |
The printMe()
function is non-abstract since it contains an implementation.
Additionally, we can also declare the property, provided we set accessor methods to it.
1 2 3 4 5 6 7 8 9 |
interface Second { val i: Int get() = 10 var str: String get() = "You Name" set(value) {} } |
We cannot use the backing field
from Properties in interfaces since an interface doesn’t store any state and hence no previous values.
Implementing Interfaces
The above interfaces that we’ve defined can be implemented over Kotlin Class as shown below.
1 2 3 4 5 6 7 8 9 10 |
class A : First { override var str: String = "str" get() = "Value is $field" override val i: Int = 1 override fun stringEcho(): String { return "Hello" } } |
We need to implement all the abstract properties of the interface or else set the class as an abstract class
.
An override
modifier overrides the property/function from the interface defined.
Implementation of non-abstract properties/functions is not necessary.
Let’s initialize the class in the main function.
1 2 3 4 5 6 7 8 9 |
fun main(args: Array<String>) { var a = A() a.printMe() a.str = "Kotlin Interfaces" println(a.str) println(a.stringEcho()) } |
Overriding properties inside the constructor
Properties can be overriden as constructor parameters of the class too.
1 2 3 4 5 6 7 |
class B(override var str: String, override val i: Int) : First{ override fun stringEcho(): String { TODO("not implemented") //To change body of created functions use File | Settings | File Templates. } } |
Implementing Multiple Interfaces
A class can implement multiple interfaces too using the above concept.
Let’s look at an example wherein both the interfaces have the same functions defined as shown below.
1 2 3 4 5 6 7 8 9 10 11 12 |
interface Formal { fun echoName(username: String): String { return "Mr. $username" } } interface Informal { fun echoName(username: String): String { return username } } |
In this case, if at all we override the function implementation in our class, our class would have a single common overridden implementation as shown below.
1 2 3 4 5 6 7 |
class D : Formal, Informal { override fun echoName(username: String): String { return username } } |
What if we need to access the function from the interface?
We use the keyword super
.
Now in the above code, using super
would give a compile time error since the compiler doesn’t know which of the interfaces to goto.
So in such cases in Kotlin, we need to specify the interface along with super
as shown below.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
class D : Formal, Informal { override fun echoName(username: String): String { return super<Formal>.echoName(username) } } fun main(args: Array<String>) { val d = D() println(d.echoName("Jake Wharton")) } //Prints //Mr. Jake Wharton |
Another example:
1 2 3 4 5 |
override fun echoName(username: String): String { return "Formal : ${super<Formal>.echoName(username)}. Informal : ${super<Informal>.echoName(username)}" } |
That’s all for kotlin interface tutorial.
Reference: Official Documentation