Effective Kotlin: Item 20 — Prefer interfaces to abstract classes

As item 20 of Joshua Bloch’s excellent book Effective Java covers, interfaces in Java 8 with default methods are an incredibly powerful tool allowing you to provide implementations for some instance methods. It is easy to retrofit an existing class with an interface, and of course, a class can implement many interfaces providing a form of multiple inheritance not achievable with abstract classes.

Kotlin’s interfaces are very similar to those in Java 8, allowing the declaration of abstract methods, method implementations, abstract properties and property accessor implementations. Interfaces cannot store state and as such any properties defined don’t have backing fields and their definitions overridden by an implementation so should be used with caution. The code below demonstrates the main features of an interface:

interface AnInterface {
val abstractProperty: Boolean

var propertyAccessorImplementations: Boolean
get() = true
set
(value) {
if (value) {
// do something
}
}

fun abstractMethod()

fun methodImplementation() {
// do something
}
}

It is important to note however that Kotlin’s method implementations are synthetic and don’t compile down to Java 8’s default methods. As such implementing the above, AnInterface, in Java may result in a compile error as you need to implement AnInterface::methodImplementation.

The latest versions of Kotlin introduce the experimental @JvmDefault annotation to support generating default interface methods for JVM 1.8+ to help alleviate the problem discussed above. The feature is disabled by default, as discussed in issue KT-4779, with a compiler flag to enable it:

compileKotlin {
kotlinOptions {
freeCompilerArgs = ['-Xjvm-default=enable']
}
}

To summarise, as with Java 8, prefer interfaces over abstract classes paying close attention to Java interoperability.

Each week I am looking at “items” from Joshua Bloch’s well-respected book, Effective Java to see how it applies to Kotlin. You can find the rest of the items I’ve covered at Effective Kotlin. Please let me know your thoughts.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store