WEB Button DEVELOPER SITE
HTMLCSSJAVASCRIPTSQLPHPBOOTSTRAPJQUERYANGULARXML
 

Kotlin - Interface

Kotlin - Interface is similar to Java 8 .

nterface can be implemented by a class in order to use its defined functionality

The keyword "interface" is used to define an interface in Kotlin as shown in the following piece of code.

interface ExampleInterface {
   var myVar: String     // abstract property
   fun absMethod()       // abstract method
   fun sayHello() = "Hello World!" // method with default implementation
}
Look at the function named "sayHello()", which is an implemented method.

In the following example, we will be implementing the above interface in a class.

interface ExampleInterface  {
   var myVar: Int            // abstract property
   fun absMethod():String    // abstract method
   
   fun hello() {
      println("Hello there, Welcome to Welookups.Com!")
   }
}
class InterfaceImp : ExampleInterface {
   override var myVar: Int = 25
   override fun absMethod() = "Start Learning "
}
fun main(args: Array) {
   val obj = InterfaceImp()
   println("My Variable Value is = ${obj.myVar}")
   print("Calling hello(): ")
   obj.hello()
   
   print("Message from the Website-- ")
   println(obj.absMethod())
}
Output
Calling hello(): Hello there, Welcome to Welookups.Com!
Message from the Website-- Start Learning 

Kotlin doesn’t support multiple inheritances

In the following example, we will create two interfaces and later we will implement both the interfaces into a clas

interface A {
   fun printMe() {
      println(" method of interface A")
   }
}
interface B  {
   fun printMeToo() {
      println("I am another Method from interface B")
   }
}

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

fun main(args: Array) {
   val obj = multipleInterfaceExample()
   obj.printMe()
   obj.printMeToo()
}

In the above example, we have created two sample interfaces A, B and in the class named "multipleInterfaceExample"

method of interface A
I am another Method from interface B