O'Reilly logo

Android Development with Kotlin by Igor Wojda, Marcin Moskala

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Interfaces

Kotlin interfaces are similar to Java 8 interfaces and in contrast to interfaces from previous Java versions. 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 like in Java:

    class User:EmailProvider { 
        override fun validateEmail() { 
            //email validation 
        } 
    } 

The question may arise of how to extend a class and implement an interface at the same time. Simply place the class name after the colon, and use comma character to add one or more interfaces. It's not required to place the super class ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required