The interfaces in Kotlin will allow you to reuse more code than what you can get with Java.

The reason is very simple: you can add code to your interfaces. If you’ve tried Java 8, it’s something pretty similar.

The good thing about being able to include code in an interface is that you can use composition in a much more powerful way. We’ll see in a minute.

Interfaces in Java 6

The problem with Java interfaces is that we can only describe behavior, but not implement it.

This is enough in many cases. But there are situations that we can’t solve, because it forces us to delegate the implementation of this interface in individual objects if we want to achieve a good composition.

All this makes something as simple composing the code of a class using reusable pieces rather complicated.

Interfaces in Kotlin

Kotlin brings us some very good news: interfaces can have code.

This means that we can implement a kind of multiple inheritance (somewhat limited, in any case). We can make a class implement several interfaces, and inherit the behavior from each one.

To write an interface that includes some methods implemented, you don’t need anything special:

We could have another interface 2 with another function:

And a class that implements them could use both without problem:

Great! This gives us much more versatility when organizing our code.

Interfaces can’t keep state

It’s an important limitation to keep in mind. We can have code but not state.

This means that we can’t create a property and store the state in it. If we define a property in an interface, the class that implements it needs to overwrite it.

Let’s see an example. Imagine that the interface needs a context:

The code is simple. It’s an interface that implements a method that displays a Toast. It requires a context to do that.

If we have an activity that wants to use this interface, it needs to overwrite the context:

As simple as that. We assign the Activity itself as a context, and the interface will use it.

Now you can use the Toaster functions in the Activity without any problems.

Interface delegation

Another very interesting feature in Kotlin is the interface delegation. It is a very powerful tool to achieve a cleaner composition.

Imagine that you have a class C, composed of two objects of type A and B:

Class C uses functions A and B within its own code.

If an object is composed by other components, it’d be very good that it could use their functions directly.

There is another way to write this code and get the same result, using the interface delegation:

You can see that class C is implementing A and B, but it’s actually delegating the implementation to the objects it receives as a parameter.

By using interface delegation, the class can use functions from implemented classes directly and still delegate the implementation to other objects.

Conclusion

We’ve seen the differences between Java interfaces and those of Kotlin. Now try to find out what situations can simplify your life, because these new ideas open a world of possibilities.

Your code will be more reusable than before and much more readable.

Still not convinced about Kotlin for Android? Start using it as soon as possible! Thanks to the the previous articles you can learn more about Kotlin, or in the book where you’ll do it by creating a complete App from scratch.

Author: Antonio Leiva

I’m in love with Kotlin. I’ve been learning about it for a couple of years, applying it to Android and digesting all this knowledge so that you can learn it with no effort.

Shares
%d bloggers like this: