Module 02: Kotlin Fundamentals¶
← Module 01: Introduction | Topic Home | Next → Module 03: Kotlin Advanced
Object-oriented programming in Kotlin: classes, interfaces, inheritance, sealed classes, data classes, objects, the collections API, and generics.
Table of Contents¶
Overview¶
Module 01 gave you the vocabulary and toolset of Kotlin. Module 02 builds on that to teach you how Kotlin models the real world through its object-oriented features — and where those features deliberately diverge from Java's model.
Kotlin's OOP system has several features that Java lacks or handles awkwardly: data classes that auto-generate structural equality, sealed class hierarchies that make state machines type-safe, object declarations for true singletons, companion objects as a replacement for static members, and a collection API that distinguishes read-only from mutable views.
The generics section explains type parameters, variance (out/in), and reified type parameters — the Kotlin tools for writing type-safe generic code without Java's ceremony.
[!NOTE] This module is a stub. Full content will be added in a future update. See the Kotlin Official Documentation for coverage of these topics while the full module is being written.
Prerequisites¶
- Module 01: Introduction to Kotlin — null safety,
val/var, functions, basic syntax
Objectives¶
By the end of this module, you will be able to:
- Define Kotlin classes with primary and secondary constructors,
initblocks, and properties - Use
data class,object,companion object, andenum classappropriately - Model algebraic data types using
sealed classhierarchies - Implement interfaces and use delegation (
by) for interface composition - Use Kotlin's collections API:
map,filter,flatMap,groupBy,fold, andreduce - Write generic functions and classes with type parameters, bounds, and
in/outvariance - Use reified type parameters in inline functions to access generic types at runtime
Theory¶
Full theory content coming soon. Topics to be covered:
4.1 Classes and Constructors¶
Primary constructors,
initblocks, secondary constructors, property declarations in constructors.4.2 Data Classes, Object Declarations, and Companion Objects¶
Auto-generated methods,
copy(),objectsingletons,companion objectas static replacement.4.3 Sealed Classes and Enums¶
Algebraic data types, exhaustive
when,enum classwith properties and methods.4.4 Interfaces and Delegation¶
Interface default methods, multiple interface implementation,
byfor interface delegation.4.5 Inheritance¶
openclasses,override,abstract, why Kotlin classes are final by default.4.6 Collections API¶
Immutable vs mutable collections, transformation functions, lazy sequences.
4.7 Generics¶
Type parameters,
wherebounds,in/outvariance,reifiedwithinline.
Key Concepts¶
- data class — generates
equals,hashCode,toString,copyfrom constructor parameters. See [[kotlin-spring-android#data-class]]. - sealed class — restricted hierarchy; compiler knows all subclasses; enables exhaustive
when. See [[kotlin-spring-android#sealed-class]]. - companion object — singleton associated with a class; replaces Java
static. See [[kotlin-spring-android#companion-object]]. - reified type — allows accessing a generic type parameter at runtime inside
inlinefunctions. See [[kotlin-spring-android#reified-type]]. - delegation — the
bykeyword forwards interface calls to a delegate object. See [[kotlin-spring-android#delegation]].
Examples¶
Full worked examples coming in the complete module. Preview:
// Sealed class for a payment result
sealed class PaymentResult
data class PaymentSuccess(val transactionId: String, val amount: Double) : PaymentResult()
data class PaymentFailure(val code: Int, val message: String) : PaymentResult()
object PaymentPending : PaymentResult()
fun handlePayment(result: PaymentResult) = when (result) {
is PaymentSuccess -> println("Paid: ${result.transactionId}")
is PaymentFailure -> println("Error ${result.code}: ${result.message}")
is PaymentPending -> println("Awaiting confirmation...")
}
Common Pitfalls¶
Full pitfalls section coming in the complete module. Key pitfalls to watch for:
- Mutable data class fields —
data classwithvarproperties breaks immutability contracts- Comparing data classes by reference — use
==(structural equality), not===(referential)- Forgetting
openon classes intended for inheritance — Kotlin classes are final by default- Confusing
object(singleton) withcompanion object(class-scoped singleton)
Cross-Links¶
- [[kotlin-spring-android/modules/01_introduction]] — Null safety and basic syntax prerequisites
- [[kotlin-spring-android/modules/03_kotlin-advanced]] — Extension functions and DSLs build on sealed classes and generics
- [[kotlin-spring-android/modules/05_spring-data-and-persistence]] — JPA entities use Kotlin class features; data classes are not ideal for JPA
Summary¶
Full summary coming in the complete module. Key takeaways:
- Kotlin classes are
finalby default — useopento allow inheritancedata classis for value objects; avoid using it for JPA entitiessealed classis the idiomatic way to model algebraic data typesobjectis a true singleton;companion objectprovides class-scoped members- Kotlin collections distinguish read-only (
List) from mutable (MutableList) views- Generics support
in/outvariance for type-safe covariant and contravariant parameters