Chapters

Hide chapters

Kotlin Apprentice

Third Edition · Android 11 · Kotlin 1.4 · IntelliJ IDEA 2020.3

Before You Begin

Section 0: 4 chapters
Show chapters Hide chapters

Section III: Building Your Own Types

Section 3: 8 chapters
Show chapters Hide chapters

Section IV: Intermediate Topics

Section 4: 9 chapters
Show chapters Hide chapters

17. Interfaces
Written by Tori Gonda

Heads up... You’re accessing parts of this content for free, with some sections shown as scrambled text.

Heads up... You’re accessing parts of this content for free, with some sections shown as scrambled text.

Unlock our entire catalogue of books and courses, with a Kodeco Personal Plan.

Unlock now

You’ve learned about two Kotlin custom types: Classes and objects. There’s another custom type that’s quite useful: Interfaces.

Unlike the other custom types, interfaces aren’t anything you instantiate directly. Instead, they define a blueprint of behavior that concrete types conform to. With an interface, you define a common set of properties and behaviors that concrete types go and implement. The primary difference between interfaces and other custom types is that interfaces themselves cannot contain state.

In this chapter, you’ll learn about interfaces and see why they’re central to programming in Kotlin. Open up a Kotlin file to get going.

Introducing interfaces

You define an interface much as you do any other custom type. Add this interface to your Kotlin file, outside the main() function:

interface Vehicle {
  fun accelerate()
  fun stop()
}

This creates a Vehicle interface with blueprint instructions for two methods: accelerate() and stop().

The keyword interface is followed by the name of the interface, followed by curly braces with the members of the interface inside. The big difference you’ll notice is that the interface doesn’t have to contain any implementation.

That means you can’t instantiate a Vehicle directly:

Interface Vehicle does not have constructors
Interface Vehicle does not have constructors

Instead, you use interfaces to enforce methods and properties on other types. What you’ve defined here is something like the idea of a vehicle — it’s something that can accelerate and stop.

Interface syntax

An interface can be implemented by a class or object, and when another type implements an interface, it’s required to define the methods and properties defined in the interface. Once a type implements all members of an interface, the type is said to conform to the interface.

Blakv Ocizlxju ab tus urpxrunq egv kium pah ijgkubasj igbljeyh rojbac hroc hipuyeq un Mifalne
Qhelk Uwodpqvo iy yiy olwfruvn awl yeon pah ahbcaboxg amfhcokl xiznuj hdit yoqiyok iw Zamarbu

Methods in interfaces

In the Vehicle interface above, you define a pair of methods, accelerate() and stop(), that all types conforming to Vehicle must implement.

Default method implementations

Just as you can in Java 8, you can define default implementations for the methods in an interface.

Properties in interfaces

You can also define properties in an interface. Add this interface:

Interface inheritance

The Vehicle interface contains a set of methods that could apply to any type of vehicle, such as a bike, a car, a snowmobile or even an airplane!

Mini-exercises

  1. Create an interface Area that defines a read-only property area of type Double.
  2. Implement Area with classes representing Square, Triangle, and Circle.
  3. Add a circle, a square, and a triangle to an array. Convert the array of shapes to an array of areas using map.

Implementing multiple interfaces

One class can only inherit from another single class. This is the property of single inheritance. In contrast, a class can adopt as many interfaces as you’d like!

Interfaces in the standard library

The Kotlin standard library uses interfaces extensively in ways that may surprise you. Understanding the roles interfaces play in Kotlin can help you write clean, decoupled “Kotliny” code.

Iterator

Kotlin lists, maps, and other collection types all provide access to Iterator instances. Iterator is an interface defined in the Kotlin standard library, and declares methods next(), which should give the next element of the collection, and hasNext(), which returns a boolean indicating whether the collection has more elements.

Comparable

Comparable declares an operator function used to compare an instance to other instances.

Challenges

Create a collection of interfaces for tasks at a pet shop that has dogs, cats, fish and birds.

Key points

  • Interfaces define a contract that classes, objects, and other custom types can implement.
  • By implementing an interface, a type is required to conform to the interface by implementing all methods and properties of the interface.
  • A type can implement any number of interfaces, which allows for a quasi-multiple inheritance not permitted through subclassing.
  • The Kotlin standard library uses interfaces extensively. You can use many of them, such as Comparable, on your own types.

Where to go from here?

Interfaces help you decouple behavior from implementation. Since interfaces are types themselves, you can still declare an array of Vehicle instances. The array could then contain bicycles, trucks, or cars. In addition, bicycles could be enumerations and trucks could be classes! But every Vehicle has a particular set of properties and methods you know you must implement.

Have a technical question? Want to report a bug? You can ask questions and report bugs to the book authors in our official book forum here.
© 2026 Kodeco Inc.

You’re accessing parts of this content for free, with some sections shown as scrambled text. Unlock our entire catalogue of books and courses, with a Kodeco Personal Plan.

Unlock now