Multiple interfaces

Kotlin does not allow multiple inheritance, but it does allow classes with more than one interface. To understand the concept of multiple interfaces, let's take the example of a Calculator class. There are few basic operations that every calculator performs—add, subtract, multiply, and divide. We can create an interface for each operation and let the class implement as many interfaces as it wants:

interface IAdd {
fun add(a : Int, b : Int)
}

interface ISubtract {
fun subtract(a : Int, b : Int)
}

interface IMultiply {
fun multiply(a : Int, b : Int)
}

interface IDivide{
fun divide(a : Int, b : Int)
}

Now, each class must follow the rules and define the same function name and parameters that are defined in the interface. This is one of the benefits of interfaces—classes are bound to follow the rules that are mentioned in the interface, instead of providing their own definitions. We can now create a Calculator class and implement more than one interface in our class. To do this, the name of the interfaces will be separated with a comma:

class Calculator() : IAdd, ISubtract {

override fun subtract(a: Int, b: Int) {
println("$a - $b = ${a-b}")
}

override fun add(a: Int, b: Int) {
println("$a + $b = ${a+b}")
}
}

fun main(args: Array<String>) {
val calc = Calculator()
calc.add(5, 4)
calc.subtract(5, 4)
}

We can add as many interfaces as we want. An interface can also extend other interfaces and provide its own function definition. In the following example, the InterfaceBasicCalculator interface extends the other interfaces:

interface InterfaceBasicCalculator : IAdd, ISubtract, IMultiply, IDivide {
fun displayMessage()
}

When a class implements this interface, it has to provide an implementation of all the functions that are defined in the extended interfaces:

 class Calculator() : InterfaceBasicCalculator {
override fun multiply(a: Int, b: Int) {
}
::::
override fun displayMessage() {
println("All functions are implemented")
}
:::::
}

As well as function signatures, interfaces are also able to provide function implementations. Let's create an interface called IDriveable, which can be implemented by the Car class. This interface might have many function signatures, such as MoveForward, tunLeft, turnRight, or engineStart. For simplicity, we are going to define two functions—engineStart and moveForward. We will provide an implementation of each function within this interface. Let's implement the startEngine function first:

interface IDriveable {

fun startEngine(){
println("Engine is ready ...")
}
fun moveForward()
}

Create a Car class with a property name and implement the IDriveable interface. Notice that the Kotlin compiler only throws an error for the moveForward() function implementation because the startEngine function is already implemented in the IDriveable class:

class Car(val name : String) : IDriveable {

override fun moveForward() {
println("$name is driving on the road")
}
}

We can override the function with our new implementation. We can also utilize functions that have already been implemented from the interface. The super keyword is used to call the function from the interface:

class Car(val name : String) : IDriveable {

override fun moveForward() {
println("$name is driving on the road")
}

override fun startEngine() {
super.startEngine()
println("Turbo technology is activated ...")
}
}

We can also declare class properties within the interface but we cannot assign any value to them because the interface does not contain any state. Let's add an integer property called numberOfDoors and implement it in the Car class using the override keyword:

interface IDriveable {
val numberOfDoors : Int
fun startEngine()
fun moveForward()
}

class Car(val name : String, override val numberOfDoors: Int) : IDriveable {
override fun moveForward() {
println("$name is driving on the road")
}
override fun startEngine() {
println("Turbo technology is activated ...")
}
}

fun main(args: Array<String>) {
val tesla = Car("Tesla" , 4)
tesla.startEngine()
tesla.moveForward()
}

In the following section, we will look at what happens if a class implements more than one interface and each interface contains functions with the same name.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.226.187.233