liveBook · Manning

Superclass

As explained in Kotlin in Action, 2E

A base class is a class from which one or more subclasses inherit members and behavior.

A superclass is the class a subclass extends to inherit members and behavior. In Kotlin, inheritance is deliberately opt-in: classes and members are final by default, so a class must be marked `open` before other classes can derive from it. Mark only the members intended for customization as `open`; this limits the fragile base class problem, where superclass changes break subclass assumptions. section 4.1.2

open class User(val nickname: String) { /* ... */ }

class SocialUser(nickname: String) : User(nickname) { /* ... */ }

Place the superclass after `:`. When its primary constructor needs arguments, invoke it in the subclass header with those arguments. section 4.2.1

A superclass constructor must be initialized. Even a superclass with no constructor parameters needs `()` when inherited: `class RadioButton : Button()`. Interfaces differ: they have no constructors, so their names appear in the supertype list without parentheses. section 4.2.1

When a subclass uses secondary constructors, every construction path must eventually initialize the superclass:

  • Call the matching superclass constructor with `super(arguments)` when that constructor directly supplies the base-class state. section 4.2.2
  • Delegate to another constructor in the same class with `this(arguments)` when it can prepare or convert arguments; that delegated constructor must ultimately call `super(...)`. section 4.2.2
  • Use secondary constructors chiefly for Java interoperability or genuinely different creation parameter lists. section 4.2.2

Control what a subclass may change

`RichButton` can expose an overridable `animate()` while keeping `disable()` non-overridable. A `ThemedButton` may override the allowed behavior, and an overridden member remains open unless it is explicitly marked `final`. Abstract classes can also require subclasses to provide abstract properties or functions. section 4.1.2

Superclass is related to, but not identical with, supertype. A subtype can be used wherever its supertype is required. In simple class inheritance, a subclass gives a subtype, but implemented interfaces and nullable types also create subtype relations without describing a different superclass. section 11.3.2

How Kotlin in Action, 2E covers it

How deep it goes
Explains open and final inheritance rules plus primary and secondary constructor delegation paths.
Examples
Uses User and SocialUser, Button and RadioButton, RichButton and ThemedButton, and Downloader constructor paths.
What you can do after
Apply it via Kotlin inheritance syntax to control extension points and initialize an appropriate superclass constructor.
How it connects
Connects inheritance with subclasses, constructors, interfaces, overriding, abstract classes, subtypes, and nullable types.

Also explained in

Compare how 10 books explain superclass →

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.