Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Kotlin Quick Start Guide

You're reading from   Kotlin Quick Start Guide Core features to get you ready for developing applications

Arrow left icon
Product type Paperback
Published in Aug 2018
Publisher Packt
ISBN-13 9781789344189
Length 178 pages
Edition 1st Edition
Languages
Tools
Arrow right icon
Author (1):
Arrow left icon
Marko Devcic Marko Devcic
Author Profile Icon Marko Devcic
Marko Devcic
Arrow right icon
View More author details
Toc

Interfaces


If you have any experience with any modern language, then you have probably used a type that defines a behavior. These types are called traits in Scala, protocols in Swift, and interfaces in Kotlin, Java, and C#.

An interface is a blueprint or a definition of a type. When a type implements an interface, we can then refer to it by this contract, that is, a set of methods that the type implements.

Here is an interface declaration in Kotlin:

interface Drivable {
fun drive()
}

The syntax for implementing an interface is the same as for inheritance. In the implementing class header, after a primary constructor or a class name comes the colon and the interface name:

class Car : Drivable {
override fun drive() {
println("Driving a car")
    }
}

The only difference from inheritance syntax is that we don't call the base type constructor because interfaces don't have one.

The implementing type (unless it is an abstract class) has to implement or override all the members that interface defined...

lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime
Banner background image