Interface in Kotlin and when to use it

Boonya Kitpitak
The OOZOU Blog
Published in
3 min readJul 5, 2018

After using Kotlin in Android development for a while, I’ve just realized the benefit of Interface in Kotlin. In Java 6, Interface can only be used to describe the behaviors, but not implement them.

Fortunately, Kotlin allows Interface to have code which means a class can implement an Interface, and inherit the behavior from it.

To begin with, if you come from Java, you might have an interface like this.

Let’s have Aeroplane and Helicopter implement the Interface like this.

Nothing wrong with them right ? However, as Kotlin allows us to have code in the Interface, we can define a property to be overridden and implement funtion in the Interface like this

Then we just have to override the property in our classes as the followings.

When we want to start engine of our Aeroplane or Helicopter, we can directly call the function like this!

You may wonder how could we use this in action (more than just start the engine of aeroplane and helicopter)

I’ll take the example derived from part of the book called Kotlin for Android Developer by Antonio Leiva. It’s really good book for every Android Developer who want to start using Kotlin.

Basically, we are going to create an interface that help us

  • Set the toolbar title
  • Handle the click on setting menu

Then in your MainActivity or other activities that need setting menu, we can make them implement our interface.

As you can see that the interface helps us separate some common code from activities and it makes you code look a bit cleaner. Moreover, we can reuse our interface in multiple places so that we don’t need to write the same code over and over again.

However, interface is not always a suitable solution to simplify our code. Let’s look at the next example.

Let’s create the interface that help us simplify the process of creating Toast

An activity that implement the interface might look like this.

Everything seem to be fine with the above example. The ToastManager is doing its job well ,but in this case we have an easier solution. We can use Kotlin Extension!!

Instead of create the interface like what we have done, we could write extensions functions.

As you can see in the example below that without implementing interface and overriding context , we can access the functions longToast because our ToastActivity has extended AppCompatActivity which consider to be Activity instance.

Discussion & Conclusion

Interface vs Extension

In my opinion, if the functionality you want to achieve is common to every instance of the particular Class, using Extension is a way to go. However, when you want a behavior that is more specific, Interface is consider to be more appropriate like SettingToolbarManager in the above example.

This is just my own thought. Please suggest if you don’t agree. :)

Anyway Interface is a great way to simplify your code and avoid repeating yourself.

Source

--

--

Boonya Kitpitak
The OOZOU Blog

Android Developer at Oozou. Also Guitarist and Headbanger