Classes, Objects, and Interfaces
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
openkeyword, visibility modifiers includinginternal, nested vs inner classes, sealed classes for exhaustivewhen, primary/secondary constructors andinitblocks, interface properties and backing fields,dataclasses andbydelegation, and the three uses ofobject(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
overrideandsuper<Type>calls. - Understand why Kotlin classes and members are final by default to avoid fragile base-class issues, and open them with
openwhen inheritance is intended. - Apply Kotlin’s visibility modifiers (public, private, protected, and
internal) and explain howinternalscopes 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
whenexpression can be made exhaustive without anelsebranch. - Write concise primary constructors in the class header, place initialization logic in
initblocks, and provide secondary constructors that delegate withthis(...)when multiple construction paths are necessary. - Implement interface properties with either stored or computed properties, and access a property’s backing field via
fieldinside an accessor, knowing that a backing field is generated only when an accessor references it. - Declare value objects with
datato auto-generatetoString,equals,hashCode,copy, andcomponentNfunctions, and forward interface implementations usingbyto delegate behavior without manual boilerplate. - Use the single
objectkeyword 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