Effective Kotlin: Item 21 — Design interfaces for posterity

Java 8 introduced default methods to help with the addition of new methods to existing interfaces. However, as Joshua Bloch’s Effective Java points out this is still fraught with risk. The book gives a real-world example of the addition of removeIf where particular Collection implementations will fail at runtime.

With Kotlin we are no more protected from the same risks highlighted, but additionally, we also need to consider Java interoperability. As presented in item 20, Kotlin’s default implementations by design are not based on Java 8’s default methods unless you enable the @JvmDefault annotation. As the documentation states though, methods with the annotation are “excluded from interface delegation” making composition harder (item 18). Effectively there is a trade-off with Java 8 integration whatever you choose.

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