← Back to Kotlin Course | Chapter 7: Inheritance & Interfaces | Lesson 2 of 7

The override Keyword

The override keyword is how you tell Kotlin 'yes, I'm intentionally replacing this inherited behavior with my own version'.
Syntax
kotlin
open class BaseClass {
    open fun method() { }
    open val property: Type = value
}

class SubClass : BaseClass() {
    override fun method() { super.method() }
    override val property: Type = otherValue
}

Overriding a Function

Using override fun in a subclass replaces the base class's open implementation with a new one, and the keyword is mandatory in Kotlin, making overrides explicit and easy to spot.

Example: Overriding a Function

markup
open class Shape {
    open fun area(): Double = 0.0
}

// Define a class `Square`
class Square(val side: Double) : Shape() {
    override fun area(): Double = side * side
}

// Entry point: execution of the program starts here
fun main() {
    // Declare a read-only variable `square`, set to `Square(4.0)`
    val square = Square(4.0)
    // Print "Area: ${square.area()}" to the console, with a trailing newline
    println("Area: ${square.area()}")
}

Calling the Base Implementation with super

super.member inside an override calls the base class's original version, letting a subclass extend rather than completely replace the inherited behavior.

Example: Calling the Base Implementation with super

markup
open class Logger {
    open fun log(message: String) {
        // Print "[LOG] $message" to the console, with a trailing newline
        println("[LOG] $message")
    }
}

// Define a class `TimestampedLogger`
class TimestampedLogger : Logger() {
    override fun log(message: String) {
        super.log("2024: $message")
    }
}

// Entry point: execution of the program starts here
fun main() {
    // Declare a read-only variable `logger` (a `Logger`), set to `TimestampedLogger()`
    val logger: Logger = TimestampedLogger()
    logger.log("Application started")
}

Overriding Properties

Properties can be overridden just like functions, using override val/override var, as long as the base property was declared open.

Example: Overriding Properties

markup
open class Vehicle {
    open val maxSpeed: Int = 100
}

// Define a class `SportsCar`
class SportsCar : Vehicle() {
    override val maxSpeed: Int = 300
}

// Entry point: execution of the program starts here
fun main() {
    // Declare a read-only variable `car` (a `Vehicle`), set to `SportsCar()`
    val car: Vehicle = SportsCar()
    // Print "Max speed: ${car.maxSpeed}" to the console, with a trailing newline
    println("Max speed: ${car.maxSpeed}")
}

Preventing Further Overrides with final

Adding final before an override member prevents any further subclass from overriding it again, locking that particular behavior in place at this level of the hierarchy.

Example: Preventing Further Overrides with final

markup
open class Base {
    open fun greet() = "Hello from Base"
}

open class Middle : Base() {
    final override fun greet() = "Hello from Middle"
}

// Entry point: execution of the program starts here
fun main() {
    // Declare a read-only variable `middle` (a `Base`), set to `Middle()`
    val middle: Base = Middle()
    // Print `middle.greet()` to the console, with a trailing newline
    println(middle.greet())
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
Common Mistakes
  1. Forgetting override when replacing an inherited open member; Kotlin requires it explicitly, unlike Java's optional @Override.
  2. Using override on a member that was not declared open in the base class, which the compiler rejects.
  3. Forgetting to call super.method() when the overriding method should extend, not fully replace, the base behavior.
Chapter Summary
  • Kotlin requires the override keyword whenever a subclass replaces an inherited open member.
  • The base member being overridden must itself be marked open (or be an abstract/interface member).
  • super.member calls the base class's original implementation from within an override.
  • An overriding member is itself open for further overriding in a deeper subclass, unless marked final.
🔒

Chapter Quiz — Complete all 7 topics to unlock

0/7 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.