Use Kotlin Classes

May 22 2024 · Kotlin 1.9.24, Android 14, Kotlin Playground

Lesson 05: Override Methods

Demo

Episode complete

Play next episode

Next

Heads up... You’re accessing parts of this content for free, with some sections shown as obfuscated text.

Heads up... You’re accessing parts of this content for free, with some sections shown as obfuscated text.

Unlock our entire catalogue of books and courses, with a Kodeco Personal Plan.

Unlock now

In this demo, you’ll see what it takes to access your superclass’s full power.

open class Vehicle {
  open fun startEngine() {
    // Perform generic engine checks
    println("Performing generic engine checks")
  }
}

class ElectricCar(val batteryLevel: Double) : Vehicle() {
  override fun startEngine() {
    super.startEngine() // Call superclass checks first
    if (batteryLevel > 0.2) {
      println("Electric car: Battery level sufficient, ready to start!")
    } else {
      println("Electric car: Low battery, cannot start!")
    }
  }
}

fun main() {
  val tesla = ElectricCar(0.1)
  tesla.startEngine()
}
See forum comments
Cinema mode Download course materials from Github
Previous: Instruction Next: Conclusion