In this tutorial you will learn about the Kotlin Interfaces and its application with practical example.
Kotlin Interfaces
The Kotlin Interface can contain method implementation and also abstract methods declaration, It is similar to Java 8.To use interface’s defined functionality we can implement it by a class. We can define interface in kotlin by keyword “interface”.
Define interface in Kotlin: As I told you that keyword “interface” is used to define interface in kotlin, See below code.
1 2 3 4 5 6 |
interface DemoInterface { var demoVar: String // abstract property fun infAbs() // abstract method fun welcome() = "Welcome to W3adda.com" // method with default implementation } |
In above code “DemoInterface” an Interface, “demoVar” is an abstract property . infAbs () is an abstract method. The above interface also has a non-abstract method that is welcome().
Implementation of Interface: Let’s get understand that how we can implement interface by a class with the help of below simple code.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
interface DemoInterface { val demoVar: Int // abstract property fun infAbs() : String // abstract method (returns String) fun welcome() { // method with default implementation // body (optional) } } class DemoImp : DemoInterface { override val demoVar: Int = 10 override fun infAbs() = "Welcome To W3Adda" // other code } |
In above code DemoImp is a class which implements DemoInterface interface. The DemoImp class overrides abstract member demoVar and abstract method infAbs().
Example: Now let’s get know more about interface by it’s example, check below code.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
interface DemoInterface { val demoVar: Int // abstract property fun infAbs() : String // abstract method (returns String) fun welcome() { // method with default implementation println("I am welcome function ") } } class InterfaceImp : DemoInterface { override val demoVar: Int = 10 override fun infAbs() = "Welcome To W3Adda" } fun main(args: Array<String>) { val obj = InterfaceImp() println("W3Adda : demoVar = ${obj.demoVar}") println("W3Adda : Calling Welcome(): ") obj.welcome() print("W3Adda : Calling and printing infAbs(): ") println(obj.infAbs()) } |
Output: The output of this code will look like below image.
Multiple Inheritance in Kotlin: Kotlin does not provide multiple inheritance but we can allow it using interface, we can implement one or more interface in single class. See below example.
Example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
interface DemoA { fun printMe() { println("W3Adda : From interface Demo A") } } interface DemoB { fun printMeToo() { println("W3Adda : From interface Demo B") } } // implements two interfaces DemoA and DemoB by DemoChild class DemoChild: DemoA, DemoB fun main(args: Array<String>) { val obj = DemoChild() obj.printMe() obj.printMeToo() } |
Output: So output of this code will look like below image.
Issue : Now suppose we have two interface with same non-abstract method name, suppose it is printMe() method. Now see what will happen. Check below code.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
interface DemoA { fun printMe() { println("W3Adda : From interface Demo A") } } interface DemoB { fun printMe() { println("W3Adda : From interface Demo B") } } // implements two interfaces DemoA and DemoB by DemoChild class DemoChild: DemoA, DemoB fun main(args: Array<String>) { val obj = DemoChild() obj.printMe() } |
Output: It will give us error .
1 |
Error:(15, 1) Kotlin: Class 'DemoChild' must override public open fun printMe(): Unit defined in DemoA because it inherits multiple interface methods of it |
Solution : You can simply solve this problem by using below code.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
interface DemoA { fun printMe() { println("W3Adda : From interface Demo A") } } interface DemoB { fun printMe() { println("W3Adda : From interface Demo B") } } // implements two interfaces DemoA and DemoB by DemoChild class DemoChild: DemoA, DemoB{ override fun printMe() { super<DemoA>.printMe() super<DemoB>.printMe() } } fun main(args: Array<String>) { val obj = DemoChild() obj.printMe() } |
Output: Now you can see in below image the output of above code.There is no error occurred.