liveBook · Manning

Super keyword

As explained in Kotlin in Action, 2E

The super construct accesses or invokes inherited methods, constructors, or attributes according to a language’s inheritance rules.

Kotlin’s super keyword reaches an inherited implementation or initializes a superclass constructor. It is especially useful when a class inherits conflicting default methods or exposes several construction paths. section 4.1.1 section 4.2.2

Selecting an inherited interface implementation

When Clickable and Focusable both define showOff(), Button must override it explicitly. The override can invoke both implementations: class Button : Clickable, Focusable { override fun click() = println("I was clicked") override fun showOff() { super<Clickable>.showOff() super<Focusable>.showOff() } } The angle-bracketed type identifies which inherited implementation to call. If only one implementation is needed, an expression body is enough: override fun showOff() = super<Clickable>.showOff(). section 4.1.1

Use super in two closely related Kotlin situations:

  • Call a specific inherited interface method with super<Type>.method(). Kotlin requires an explicit override when multiple supertypes provide the same default method. section 4.1.1
  • Delegate a secondary constructor to a matching superclass constructor with : super(arguments). Each secondary constructor must either initialize the base class or delegate to another constructor that does. section 4.2.2
  • Delegate between constructors of the same class with : this(arguments), then let the delegated constructor call super(). section 4.2.2
Listing 4.5 Invoking an inherited interface method implementation
class Button : Clickable, Focusable {
    override fun click() = println("I was clicked")
 
    override fun showOff() {           #1
        super<Clickable>.showOff()     #2
        super<Focusable>.showOff()     #2
    }
}

Inspect the complete Button example showing qualified super calls for both inherited interface implementations. section 4.1.1

class MyDownloader : Downloader {
    constructor(url: String?) : super(url) {   #1
        // ...
    }
    constructor(uri: URI?) : super(uri) {      #1
        // ...
    }
}

Inspect the MyDownloader example showing two secondary constructors invoking different superclass constructors. section 4.2.2

The syntax differs from Java for interface defaults: Kotlin uses super<Type>.showOff(), whereas Java uses Clickable.super.showOff(). Kotlin’s secondary-constructor delegation follows the same inheritance-oriented purpose while preserving Java-oriented constructor shapes. section 4.1.1 section 4.2.2

How Kotlin in Action, 2E covers it

How deep it goes
Explains qualified interface calls, constructor delegation paths, and Java syntax differences without descending into compiler-generated bytecode.
Examples
Uses Button with Clickable and Focusable, plus MyDownloader delegating to String and URI superclass constructors.
What it assumes
Assumes familiarity with Kotlin classes, interfaces, inheritance, method overriding, and primary or secondary constructors.
What you can do after
Apply it via Kotlin syntax to select inherited interface behavior and delegate subclass construction to superclass constructors.
How it connects
Connects inheritance, interfaces, method overriding, default methods, secondary constructors, and Java interoperability.

Also explained in

Compare how 6 books explain super keyword →

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.