Classes, Objects, and Interfaces

Keywords

ver. 1.0.0

Declare Kotlin types, control inheritance and visibility, use constructors, data classes, delegation and object forms

This unit teaches Kotlin’s type and declaration features: interfaces with default implementations, final-by-default classes and the open keyword, visibility modifiers including internal, nested vs inner classes, sealed classes for exhaustive when, primary/secondary constructors and init blocks, interface properties and backing fields, data classes and by delegation, and the three uses of object (singletons, companion objects, and anonymous objects).

You’ll learn to declare Kotlin types and use the language’s declaration idioms effectively. Specifically, you’ll be able to:

  • Declare interfaces that contain both abstract members and methods with default implementations, and resolve conflicts when multiple defaults collide using explicit override and super<Type> calls.
  • Understand why Kotlin classes and members are final by default to avoid fragile base-class issues, and open them with open when inheritance is intended.
  • Apply Kotlin’s visibility modifiers (public, private, protected, and internal) and explain how internal scopes differ from Java’s package-private semantics.
  • Choose between a nested class (no implicit reference to the outer instance) and an inner class (holds a reference) and avoid Java’s defaults that can break serialization.
  • Use sealed classes to restrict hierarchies so a when expression can be made exhaustive without an else branch.
  • Write concise primary constructors in the class header, place initialization logic in init blocks, and provide secondary constructors that delegate with this(...) when multiple construction paths are necessary.
  • Implement interface properties with either stored or computed properties, and access a property’s backing field via field inside an accessor, knowing that a backing field is generated only when an accessor references it.
  • Declare value objects with data to auto-generate toString, equals, hashCode, copy, and componentN functions, and forward interface implementations using by to delegate behavior without manual boilerplate.
  • Use the single object keyword in its three roles: object declarations for singletons, companion objects as Kotlin’s replacement for static members (including naming and implementing interfaces), and object expressions for anonymous objects in place of Java’s anonymous inner classes.

After completing this unit you can confidently design and implement Kotlin classes and interfaces, control inheritance and visibility, construct objects succinctly, rely on generated equality and destructuring behavior, delegate implementations, and use object-based patterns for singletons, companions, and anonymous instances. This prepares you to move on to the language’s functional features, starting with lambdas.

Materials

Source document

  • Kotlin in Action, Second Edition, Sebastian Aigner, Roman Elizarov, Svetlana Isakova, and Dmitry Jemerov, Manning, April 2024 — Link — Page 78-118