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()

  }

}

Rispondi

Inserisci i tuoi dati qui sotto o clicca su un'icona per effettuare l'accesso:

Logo di WordPress.com

Stai commentando usando il tuo account WordPress.com. Chiudi sessione /  Modifica )

Google photo

Stai commentando usando il tuo account Google. Chiudi sessione /  Modifica )

Foto Twitter

Stai commentando usando il tuo account Twitter. Chiudi sessione /  Modifica )

Foto di Facebook

Stai commentando usando il tuo account Facebook. Chiudi sessione /  Modifica )

Connessione a %s...