Industrial Training




Kotlin Interface


An interface is a blueprint of class.Kotlin interface is similar to Java 8. It contains abstract method declarations as well as implementation of method.


Defining Interface


An interface is defined using the keyword interface. For example:


interface MyInterface {  
val id: Int // abstract property  
    fun absMethod()// abstract method  
    fun doSomthing() {  
      // optional body  
    }  
}  

The methods which are only declared without their method body are abstract by default.


Why use Kotlin interface?


Following are the reasons to use interface:


  • Using interface supports functionality of multiple inheritance.
  • It can be used achieve to loose coupling.
  • It is used to achieve abstraction.

Subclass extends only one super class but implements multiple interfaces. Extension of parent class or interface implementation are done using (:) operator in their subclass.


Implementing Interfaces


In this example, we are implementing the interface MyInterface in InterfaceImp class. InterfaceImp class provides the implementation of property id and abstract method absMethod() declared in MyInterface interface.


interface MyInterface  {  
var id: Int            // abstract property  
    fun absMethod():String    // abstract method  
    fun doSomthing() {  
println("MyInterface doing some work")  
    }  
}  
class InterfaceImp : MyInterface {  
    override var id: Int = 101  
    override fun absMethod(): String{  
return "Implementing abstract method.."  
    }  
}  
fun main(args: Array) {  
val obj = InterfaceImp()  
println("Calling overriding id value = ${obj.id}")  
obj.doSomthing()  
println(obj.absMethod())  
}  

Output:
Calling overriding id value = 101
MyInterface doing some work
Implementing abstract method..

Implementing multiple interface


We can implement multiple abstract methods of different interfaces in same class. All the abstract methods must be implemented in subclass. The other non-abstract methods of interface can be called from derived class.
For example, creating two interface MyInterface1 and MyInterface2 with abstract methods doSomthing() and absMethod() respectively. These abstract methods are overridden in derive class MyClass.


interface MyInterface1 {  
    fun doSomthing()  
}  
interface MyInterface2 {  
    fun absMethod()  
}  
class MyClass : MyInterface1, MyInterface2 {  
    override fun doSomthing() {  
println("overriding doSomthing() of MyInterface1")  
    }  
  
    override fun absMethod() {  
println("overriding absMethod() of MyInterface2")  
    }  
}  
fun main(args: Array< String>) {  
val myClass = MyClass()  
myClass.doSomthing()  
myClass.absMethod()  
}  

Output:
overriding doSomthing() of MyInterface1
overriding absMethod() of MyInterface2

Resolving different Interfaces having same method overriding conflicts


Let's see an example in which interface MyInterface1 and interface MyInterface2 both contains same non-abstract method. A class MyClass provides the implementation of these interfaces. Calling the method of interface using object of MyClass generates an error.


interface MyInterface1 {  
    fun doSomthing(){  
println("overriding doSomthing() of MyInterface1")  
    }  
}  
interface MyInterface2 {  
    fun doSomthing(){  
println("overriding doSomthing() of MyInterface2")  
    }  
}  
class MyClass : MyInterface1, MyInterface2 {  
  
}  
fun main(args: Array< String>) {  
val myClass = MyClass()  
myClass.doSomthing()  
}  

Output:
Kotlin: Class 'MyClass' must override public open fun doSomthing(): Unit defined in MyInterface1 because it 
inherits multiple interface methods of it

To solve the above problem we need to specify particular method of interface which we are calling. Let's see an example below.
In below example, two interfaces MyInterface1 and MyInterface2 contain two abstract methodsadsMethod() and absMethod(name: String) and non-abstract method doSomthing() in both respectively. A class MyClass implements both interface and override abstract method absMethod() and absMethod(name: String) . To override the non-abstract method doSomthing() we need to specify interface name with method using super keyword as super< interface_name>.methodName().

interface MyInterface1 {  
    fun doSomthing() {  
println("MyInterface 1 doing some work")  
    }  
        fun absMethod()  
}  
interface MyInterface2 {  
    fun doSomthing(){  
println("MyInterface 2 doing some work")  
    }  
    fun absMethod(name: String)  
}  
class MyClass : MyInterface1, MyInterface2 {  
    override fun doSomthing() {  
        super< MyInterface2>.doSomthing()  
    }  
  
    override fun absMethod() {  
println("Implements absMethod() of MyInterface1")  
    }  
    override fun absMethod(n: String) {  
println("Implements absMethod(name) of MyInterface2 name is  $n")  
    }  
}  
fun main(args: Array< String>) {  
val myClass = MyClass()  
myClass.doSomthing()  
myClass.absMethod()  
myClass.absMethod("Ashu")  
}  

Output:
MyInterface 2 doing some work
Implements absMethod() of MyInterface1
Implements absMethod(name) of MyInterface2 name is  Ashu

interface MyInterface1 {  
    fun doSomthing() {  
println("MyInterface 1 doing some work")  
    }  
    fun absMethod()  
}  
  
interface MyInterface2 {  
    fun doSomthing() {  
println("MyInterface 2 doing some work")  
    }  
   fun absMethod() {  
println("MyInterface 2 absMethod")  
    }  
  
}  
  
class C : MyInterface1 {  
    override fun absMethod() {  
println("MyInterface1 absMethod implementation")  
    }  
}  
  
class D : MyInterface1, MyInterface2 {  
    override fun doSomthing() {  
        super< MyInterface1>.doSomthing()  
        super< MyInterface2>.doSomthing()  
    }  
  
    override fun absMethod() {  
  
        super< MyInterface2>.absMethod()  
    }  
}  
  
fun main(args: Array< String>) {  
val d = D()  
val c = C()  
d.doSomthing()  
d.absMethod()  
c.doSomthing()  
c.absMethod()  
}  

Output:
MyInterface 1 doing some work
MyInterface 2 doing some work
MyInterface 2 absMethod
MyInterface 1 doing some work
MyInterface1 absMethod implementation



Hi I am Pluto.