Thursday, 11 January 2018

Kotlin: Interfaces

What is an Interface ?
Interfaces are the contracts in the outside world.

What it mean?
Take an example, let us assume, there is a standard for all mobile phones, as per the standard, below are the basic functionality for the mobile phones to provide. 

Vendor1 provides the functionality for the mobile in language JAVA
Vendor2 provides the functionality for the mobile in language C++
Vendor3 provides the functionality for the mobile in language JAVA
Vendor4 provides the functionality for the mobile in language C

that mean all the vendors providing the same functionality, but the way they are providing is different.

One more example is IBM JAVA, Oracle JAVA are providing the functionality for JAVA, but the way they implemented the features is different.

How to define an interface in kotlin?
You can define the interfaces using 'interface' keyword. Interfaces in kotlin are similar to interfaces in Java8.

interface InterfaceName{
 /* Function without body*/
 fun method1()
 
 /* Function with body*/
 fun mehtod2(){
 
 }

}

How to implement an interface?
kotlin class can implement one or more interfaces.

Syntax
class ClassName : Interface1, Interface2...InterfaceN{

}

Find the below working application.


HelloWorld.kt

interface Arithmetic {
 fun sum(a: Int, b: Int): Int
 fun sub(a: Int, b: Int): Int
}

class ArithmeticImpl : Arithmetic {
 override fun sum(a: Int, b: Int): Int {
  return a + b
 }

 override fun sub(a: Int, b: Int): Int {
  return a - b
 }
}

fun main(args: Array<String>) {
 var obj = ArithmeticImpl()
 
 println("Sum of 10 and 20 is : ${obj.sum(10, 20)}")
 println("Sub of 10 and 20 is : ${obj.sub(10, 20)}")
}

Output

Sum of 10 and 20 is : 30
Sub of 10 and 20 is : -10

Can a class implement more than one interface?
yes, a class can implement more than one interfaces.

class ArithmeticImpl : Arithmetic, Message {

}

In the above example, "ArithmeticImpl" class is implementing Arithmetic and Message interfaces.

Find the below working application.


HelloWorld.kt
interface Arithmetic {
 fun sum(a: Int, b: Int): Int
 fun sub(a: Int, b: Int): Int
}

interface Message {
 fun amoutMe(): String
}

class ArithmeticImpl : Arithmetic, Message {
 override fun sum(a: Int, b: Int): Int {
  return a + b
 }

 override fun sub(a: Int, b: Int): Int {
  return a - b
 }

 override fun amoutMe(): String {
  return "ArithmeticImpl class"
 }
}

fun main(args: Array<String>) {
 var obj = ArithmeticImpl()

 println("Sum of 10 and 20 is : ${obj.sum(10, 20)}")
 println("Sub of 10 and 20 is : ${obj.sub(10, 20)}")
 println(obj.amoutMe())
}

Output

Sum of 10 and 20 is : 30
Sub of 10 and 20 is : -10
ArithmeticImpl class

Can I define properties in interface?
Yes, you can define properties in interfaces. Properties can be abstract (or) have definition.


HelloWorld.kt

interface DemoInterface {
 var variable1: String
 val variable2: String
  get() = "Hello World"
}

class MyClass : DemoInterface {
 override var variable1: String = "Hello"
}

fun main(args: Array<String>) {
 var obj = MyClass()

 println("variable1 : ${obj.variable1}")
 println("variable2 : ${obj.variable2}")
}

Output

variable1 : Hello
variable2 : Hello World



Previous                                                 Next                                                 Home

No comments:

Post a Comment