Kotlin
Completed
-
Section 1: Getting Started
-
Section 2: Core Syntax
-
Section 3: Null Safety
-
Section 4: Object-Oriented Kotlin
-
Section 5: Functional Kotlin
-
Section 6: Coroutines
-
Section 7: Collections Deep Dive
-
Section 8: Type System Deep Dive
-
Section 9: Interop and Tooling
-
Section 10: Kotlin DSLs and Patterns
-
Section 11: Testing and Quality
-
Section 12: Server-Side Kotlin
-
Section 13: Practical Projects
-
Section 14: Interview Practice
-
Section 15: More Practice Exercises
-
Section 16: More Standard Library
-
Section 17: Multiplatform Kotlin
-
Section 18: kotlin.collections In Depth
-
Section 19: kotlin.text In Depth
-
Section 20: kotlin.ranges and kotlin.sequences
-
Section 21: kotlin.io and File Handling
-
Section 22: kotlinx.coroutines Deep Dive
-
Section 23: kotlin.reflect
-
Section 24: Android Development with Kotlin Overview
-
Section 25: Kotlin Multiplatform Deep Dive
-
Section 26: Kotlin for Backend Deep Dive
-
Section 27: Kotlin Design Patterns
-
Section 28: Advanced Language Features
-
Section 29: More Practice Exercises
-
Section 30: More Interview Practice
-
Section 31: Kotlin Type System Deep Dive
-
Section 32: Kotlin Null Safety Advanced
-
Section 33: Kotlin Testing Deep Dive
-
Section 34: Kotlin Build Tooling Deep Dive
-
Section 35: Kotlin Serialization
-
Section 36: Kotlin Performance Considerations
-
Section 37: Kotlin Native Overview
-
Section 38: Kotlin for Data and Scripting
-
Section 39: More Coroutines Practice
-
Section 40: More Android-Adjacent Patterns
-
Section 41: More Practical Projects
-
Section 42: More Design and Architecture Practice
-
Section 43: Kotlin Language Evolution
-
Section 44: More Interview and Review
-
Section 45: Kotlin Delegation Patterns Deep Dive
-
Section 46: Kotlin Annotations Deep Dive
-
Section 47: Kotlin for Gradle Plugin Development
-
Section 48: Kotlin Concurrency Beyond Coroutines
-
Section 49: Kotlin Compiler Internals
-
Section 50: Real-World Kotlin Case Studies
-
Section 51: Final Practice Projects
-
Section 52: Kotlin for Server-Side Reactive Programming
-
Section 53: More Practice and Drills
-
Section 54: Kotlin Security Practices
20: Inheritance and Interfaces
I want to show you a snippet of code I reviewed recently from a junior dev who was building a payment processing module. They were coming from Java, and they hit a wall that almost every Java developer hits when they first move to Kotlin.
class Payment(val amount: Double) {
fun process() {
println("Processing generic payment of $$amount")
}
}
class CreditCardPayment(amount: Double, val cardNumber: String) : Payment(amount) {
override fun process() {
println("Processing credit card $cardNumber for $$amount")
}
}
If you try to compile this, the compiler will scream at you with two different errors: first, that Payment is "final" and cannot be inherited from, and second, that process() cannot be overridden. In Java, classes are open by default. In Kotlin, it's the exact opposite.
Unlocking Classes with the Open Keyword
Kotlin assumes you don't want your classes to be inherited unless you explicitly say so. This is a design choice to prevent the "fragile base class" problem, where changing a parent class accidentally breaks a dozen children you didn't know existed. To fix the code above, we have to use the open modifier.
open class Payment(val amount: Double) {
open fun process() {
println("Processing generic payment of $$amount")
}
}
class CreditCardPayment(amount: Double, val cardNumber: String) : Payment(amount) {
override fun process() {
println("Processing credit card $cardNumber for $$amount")
}
}
Now it works. Note that I had to mark both the class and the specific function as open. If you open the class but forget to open the function, you still can't override that method. I personally find this verbosity a bit annoying at first, but once you're working on a large codebase, I promise you'll appreciate knowing exactly which parts of the hierarchy are meant to be extended.
Using Interfaces for Shared Capabilities
Inheritance is great for "is-a" relationships (a CreditCardPayment is a Payment). But what happens when you have a capability that spans across unrelated classes? For example, some payments are Refundable, but others—like a gift card or a crypto transaction—might not be. You can't put refund() in the Payment base class because then you'd be forcing every single payment type to implement a refund method, even if it's impossible for that type.
This is where interfaces come in. An interface defines a contract: "I don't care who you are, as long as you can do this."
interface Refundable {
fun refund()
}
class CreditCardPayment(amount: Double, val cardNumber: String) : Payment(amount), Refundable {
override fun process() {
println("Processing credit card $cardNumber for $$amount")
}
override fun refund() {
println("Refunding payment to card $cardNumber")
}
}
class CryptoPayment(amount: Double, val walletAddress: String) : Payment(amount) {
override fun process() {
println("Processing crypto payment from $walletAddress")
}
// No refund() here, because CryptoPayment doesn't implement Refundable
}
Deciding Between Abstract Classes and Interfaces
You'll often wonder: "If an interface can have default implementations, why do I need abstract classes?" Here is my rule of thumb: use an abstract class for identity (what the object is) and an interface for ability (what the object can do).
An abstract class is perfect when you want to share state (like that amount property in our Payment class). Interfaces cannot hold state; they can't have backing fields. If you find yourself needing to store a variable that all subclasses should use, you need a class, not an interface.
If you make a class abstract, you're telling Kotlin: "This class is incomplete. You can't instantiate it directly; you can only use it as a blueprint for other classes." This is even more powerful than open because it allows you to define abstract methods that must be implemented by any child class.
abstract class Payment(val amount: Double) {
abstract fun process() // No body here! Subclasses MUST implement this.
}📋 Practical Task
Building a Smart Home Device Hierarchy
You are designing a system to control smart home devices. Your task is to implement a hierarchy that separates the "identity" of the device from its "capabilities."
- Create an abstract class called
SmartDevice. It should have a propertydeviceName: Stringand an abstract functionturnOn(). - Create an interface called
Dimmable. It should have a functionsetBrightness(level: Int). - Implement a class
SmartLightthat inherits fromSmartDeviceand implementsDimmable. - Implement a class
SmartPlugthat inherits fromSmartDevicebut does not implementDimmable. - In your
mainfunction, create a list ofSmartDeviceobjects containing both a light and a plug. Iterate through them and callturnOn(). - Finally, write a check to see if a device is
Dimmableusing theiskeyword, and if so, callsetBrightness(50).
There are no comments for now.