Kotlin miniguide #10 Interfaces

In Kotlin interfaces are very similar to Java interfaces, but we can also provide a default implementation for an interface member:


interface IEmployee{

var name : String //abstract property

fun doWork(){

//some default implementation

}

fun doBreak() //abstract

}

Default implementation allows us to add functionality to interfaces without breaking existing code, because default implementation is not required to be implemented:


class Tester (override var name : String) IEmployee {

   override fun doBreak () {... }

}

We can inherit from interfaces that have a member with the same signature:


interface ITester{

   fun doWork () {...}

}

class Tester (override var name : String) : ITester, IEmployee{

  override fun doBreak() {

  }

  override fun doWork() {

      super<ITester>.doWork()

     super<IEmployee>.doWork()

  }

}

Lascia un commento