Interfaces
Kotlin interfaces are similar to Java 8 interfaces and different to interfaces in previous version Java. An interface is defined using the interface
keyword. Let's define an EmailProvider
interface:
interface EmailProvider { fun validateEmail() }
To implement the preceding interface in Kotlin, use the same syntax as for extending classes--a single colon character (:
). There is no implements
keyword, in contrast to Java:
class User:EmailProvider { override fun validateEmail() { //email validation } }
The question of how to extend a class may arise and implement an interface at the same time. Simply place the class name after the colon, and use a comma to add one or more interfaces. It's not required to place the superclass in the first position, although it's considered good practice:
open class Person { interface EmailProvider { fun validateEmail() } class User: Person(), EmailProvider...