Using Interfaces

Created By: Geoffrey Challen
/ Updated: 2022-05-23

This topic is one of the most important we'll cover this semester! Maybe we've said that before? But we really mean it this time. Interfaces are everywhere in computer science, and Kotlin provides a great way to start understanding what they are and how they work. So let's do it!


But first, a short but important digression into one of the Kotlin keywords that we have not yet covered: abstract. (Break out your Kotlin keyword bingo cards!) Like final, abstract can be applied to both classes and methods, with somewhat different results. Let's explore together:

Demonstrate how to use abstract on both classes and methods.

We'll return to abstract in a minute. But for now let's move on!


This lesson focuses on interfaces. Like references, interfaces are ubiquitous in computer science. But let's look at the Wikipedia definition together:

In computing, an interface is a shared boundary across which two or more separate components of a computer system exchange information. The exchange can be between software, computer hardware, peripheral devices, humans, and combinations of these. Some computer hardware devices, such as a touchscreen, can both send and receive data through the interface, while others such as a mouse or microphone may only provide an interface to send data to a given system.

Walk through the Wikipedia definition of interface. Provide some real-world and computer-based examples.

Every Object Has An Interface

Just to be clear—while interfaces have a specific meaning in Kotlin that we're exploring, every Kotlin object has an interface. Let's see how:

Discuss how every object has an interface comprising the public methods that we can call.

Limitations of Inheritance

As we've pointed out in the past, Kotlin objects can only inherit from one class. This can lead to some problems as we design our classes.

Let's consider a real example: natural ordering. Some classes has a natural ordering, meaning that there is a well-established order. For example:

  • For numeric types, lowest to highest is a natural ordering
  • For Strings, lexicographic (or dictionary) order is a natural ordering

However, not every class has such an ordering. For example, it's not clear that the Pet classes we've been creating have a natural ordering.

So this is an example of a feature that not every Kotlin object has, meaning that we shouldn't add it to Any. But whether or not an object can implement this feature doesn't seem like it should constrain what it extends. Where does that leave us?


Kotlin interfaces provide a way for classes to declare that they support certain functionality. Unlike inheritance, a single class can implement more than one interface, while still extending another class. Let's see how this works!

Show how to declare and implement interfaces in Kotlin. Be sure to point out that a Kotlin class can implement multiple interfaces and also extend another class.

interface References

Reference variables work similarly with classes that implement interfaces as they do with polymorphism and inheritance. Let's look at an example:

Create two different classes that implement this interface. Show that a variable of the the interface type can refer to both, but limits us to using methods declared by the interface.

Remember abstract?

We can achieve something similar to interfaces using abstract, but it's not quite the same. Let's examine the differences:

Explain the difference between interfaces and a fully-abstract class in terms of multiple inheritance.


As we begin with interfaces, we'll focus on using them first, and then discuss more about how to provide existing interfaces. Let's look at one example of a useful built-in Kotlin interface: Comparable.

Walk through the Comparable documentation and discuss how and why it is used.


Kotlin's Comparable interface is inherited from Java, as is what is called a parameterized interface. We've discussed type parameters previously, but we don't want to tangle with them on interfaces quite yet. So, instead, when we work with Comparable we'll use our own version called OurComparable:

This works identically to the official Comparable interface except without the type parameter.

Ordering Operators Call compareTo

Remember that when we implemented .equals, Kotlin would call it when comparing two objects for equality using the == operator:

Well, it turns out that if we implement compareTo Kotlin will also call it when comparing two objects for order using operators like <! We just have to do a bit more work to set up the method correctly. Let's see how!

Show how to set up a class so that it can be compared for order.

OurComparable Example

Now, let's write some code together that uses the OurComparable interface to make an existing (simple) algorithm more generic.

Use Comparable to make the function above able to operate on anything that implements Comparable.

Show how to complete the homework problem above. Feel free to cover multiple approaches!

Show how to complete the homework problem above. Feel free to cover multiple approaches!

More Practice

Need more practice? Head over to the practice page.