liveBook · Manning

Chapter 4: Classes, objects, and interfaces

What you will learn in chapter 4 of Kotlin in Action, 2E

Introduces concrete differences in Kotlin class declarations and the language features that make classes more flexible and concise.

After this chapter you will be able to

What it builds on

Section by section

Introduction

Introduces concrete differences in Kotlin class declarations and the language features that make classes more flexible and concise.

Key terms: interfaces, final, public, nested classes, inner, primary constructor, initialization logic, accessors, data class, delegation pattern

4.1 Defining class hierarchies

This introduction previews Kotlin class hierarchies, modifier defaults, and sealed hierarchies without explaining them.

4.1.1 Interfaces in Kotlin

This section explains how to declare and implement Kotlin interfaces, handle default-method conflicts, and use those interfaces from Java.

Key terms: interface, abstract method, state, colon, override modifier, superclass, default implementation, showOff, super<Clickable>.showOff(), Java class

4.1.2 Open, final, and abstract modifiers: Final by default

This section explains Kotlin’s final-by-default inheritance rules, how to allow or forbid overrides, and how abstract classes and final properties fit those rules.

Key terms: final, fragile base class problem, open, override, smart casts, val, custom accessor, abstract class, abstract members

4.1.3 Visibility modifiers: Public by default

This section explains Kotlin’s visibility modifiers, how they protect APIs and module boundaries, and where their behavior differs from Java.

Key terms: public, protected, private, top-level declarations, internal, module, package-private, encapsulation, explicit API mode, public API

4.1.4 Inner and nested classes: Nested by default

This section contrasts Java and Kotlin nested classes, using button-state serialization to show why an implicit reference to the outer instance matters.

Key terms: nested class, inner modifier, static nested class, this@Outer, ButtonState, Button, NotSerializableException, static

4.1.5 Sealed classes: Defining restricted class hierarchies

Explains how sealed classes and interfaces restrict a hierarchy so the compiler can check that a when expression handles every case.

Key terms: sealed modifier, sealed class, sealed interface, direct subclasses, module, when expression, exhaustive, else branch

4.2 Declaring a class with nontrivial constructors or properties

Introduces Kotlin’s distinction between primary and secondary constructors and the role of initializer blocks.

Key terms: primary constructor, secondary constructor, initializer blocks

4.2.1 Initializing classes: Primary constructor and initializer blocks

This section explains how Kotlin primary constructors initialize classes, how defaults and inheritance affect construction, and when to restrict constructor access.

Key terms: primary constructor, constructor, init, initializer block, val, default values, parameterless constructors, @JvmOverloads, superclass constructor, base class list

4.2.2 Secondary constructors: Initializing the superclass in different ways

Explains when Kotlin needs secondary constructors and how they delegate to superclass or same-class constructors.

Key terms: secondary constructor, constructor keyword, super(), primary constructor, this(), base class, default parameter values, named argument syntax, secondary constructors, Java interoperability

4.2.3 Implementing properties declared in interfaces

This section explains how classes implement interface properties, why interface properties cannot store state, and when a read-only property is preferable to a function.

Key terms: abstract property, backing field, getter, primary constructor, override, custom getter, initializer, read-only property, function

4.2.4 Accessing a backing field from a getter or setter

This section explains how custom property accessors use a backing field and when the compiler generates one.

Key terms: backing field, field, getter, setter, default accessor implementation, val, mutable property

4.2.5 Changing accessor visibility

This section shows how to keep a property publicly readable while restricting writes to its owning class.

Key terms: accessor, visibility modifier, getter, setter, private

4.3 Compiler-generated methods: Data classes and class delegation

The section explains why compiler-generated methods reduce boilerplate in Kotlin classes and identifies common methods that otherwise need mechanical implementations.

Key terms: Kotlin compiler, mechanical code generation, boilerplate code, equals, hashCode, toString, hash maps

4.3.1 Universal object methods

This section explains why a data-storing Kotlin class may need custom toString, equals, and hashCode methods, and how those methods affect string output, equality, and hash-based collections.

Key terms: toString, string representation, equals, ==, ===, reference comparison, hashCode, HashSet, hash code contract

4.3.2 Data classes: Autogenerated implementations of universal methods

Explains how Kotlin data classes generate standard methods and support copying, why read-only properties are recommended, and how they compare with Java records.

Key terms: data modifier, data class, equals, hashCode, toString, primary constructor, val, var, immutable, HashMap

4.3.3 Class delegation: Using the by keyword

The section explains how Kotlin’s class delegation replaces repetitive decorator forwarding code while allowing selected methods to be overridden.

Key terms: implementation inheritance, decorator design pattern, interface, class delegation, by keyword, override, MutableCollection

4.4 The object keyword: Declaring a class and creating an instance, combined

Introduces the object keyword’s shared meaning and briefly distinguishes its three uses.

Key terms: object keyword, class, instance, object declaration, singleton, companion object, factory methods, object expression, anonymous inner class

4.4.1 Object declarations: Singletons made easy

This section explains how Kotlin object declarations create singletons, where they are useful, and when a regular class is a better choice.

Key terms: object declaration, object keyword, initializer blocks, Comparator, nested object, sortedWith, singletons, dependency injection, unit tests, INSTANCE

4.4.2 Companion objects: A place for factory methods and static members

The section explains how companion objects provide class-level access to private members and why they are useful for factory methods.

Key terms: static, top-level functions, object declarations, companion object, private members, factory methods, private constructor, secondary constructors

4.4.3 Companion objects as regular objects

This section shows that companion objects are regular objects: they can be named, implement interfaces, support extensions, and interact with Java static members.

Key terms: companion object, Companion, JSONFactory, @JvmStatic, @JvmField, companion-object extensions, fromJSON

4.4.4 Object expressions: Anonymous inner classes rephrased

This section explains how object expressions create anonymous objects, how they differ from object declarations, and when they are useful for implementing listeners.

Key terms: object expression, anonymous object, object declaration, MouseListener, variables, final variables, single-method interface, SAM conversion, function literal, lambda

4.5 Extra type safety without overhead: Inline classes

This section shows how Kotlin inline classes give basic values distinct meanings while usually avoiding the cost of wrapper objects.

Key terms: inline classes, UsdCent, type safety, value, @JvmInline, underlying type, wrapped property, primary constructor, interfaces, computed properties

4.6 Summary

Where this is used later

Concepts explained in this chapter

← Chapter 3: Defining and calling functions Chapter 5: Programming with lambdas →

These guides are generated from the text of Manning books by Manning's knowledge graph and link to the sections they draw on. The full books are available on liveBook and at manning.com.