liveBook · Manning

Default method

As explained in Kotlin in Action, 2E

A default implementation is a member implementation provided by an interface that implementing types can inherit without redefining it.

A default method is an interface function with a body. An implementing class may inherit that behavior instead of redefining it, while still implementing abstract interface methods. section 4.1.1

Providing and resolving default behavior

interface Clickable { fun click() fun showOff() = println("I'm clickable!") } interface Focusable { fun showOff() = println("I'm focusable!") } class Button : Clickable, Focusable { override fun click() = println("I was clicked") override fun showOff() { super<Clickable>.showOff() super<Focusable>.showOff() } } Button().showOff() // I'm clickable! // I'm focusable! A class may omit showOff when only one inherited default exists. When multiple interfaces provide that method, Kotlin requires an explicit override; the implementation can call either or both inherited versions with super<Type>.

How Kotlin in Action, 2E covers it

How deep it goes
Explains declaration, inheritance, conflicts, explicit overrides, and qualified super calls, but does not descend into generated JVM mechanics.
Examples
Uses Clickable and Focusable interfaces, a Button class, showOff output, and qualified super calls.
What it assumes
Assumes basic Kotlin interfaces and method overriding, while demonstrating default implementations and conflict resolution directly.
What you can do after
Apply it via Kotlin interfaces, inheriting one default or resolving several explicitly with overrides and qualified super calls.
How it connects
Connects interfaces with inheritance, method overriding, multiple inheritance of interface behavior, and Java interoperability.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Kotlin in Action, 2E. The section links point to the parts of the book it draws on.