What if you could change just one part of your program without breaking everything else?
Why Overriding methods with override in Kotlin? - Purpose & Use Cases
Imagine you have a basic toy robot that can move forward. Now you want a new robot that can move forward but also dance. Without a clear way to change the original robot's behavior, you might try copying all its code and adding dance moves manually.
Copying and changing code for every new robot is slow and confusing. If you find a mistake in the original move code, you have to fix it in every copy. This wastes time and causes errors.
Using method overriding with override lets you keep the original robot's code safe and just change the parts you want. You tell the new robot to use the old move method but add dancing on top. This is clean, fast, and easy to manage.
class Robot { fun move() { println("Moving forward") } } class DancingRobot { fun move() { println("Moving forward") println("Dancing") } }
open class Robot { open fun move() { println("Moving forward") } } class DancingRobot : Robot() { override fun move() { super.move() println("Dancing") } }
It lets you build new versions of things by changing only what matters, making your code smarter and easier to grow.
Think of a video game where different characters share basic moves but each has special attacks. Overriding lets each character keep the basic moves and add their unique style without rewriting everything.
Overriding lets you change behavior safely without copying all code.
The override keyword clearly shows which methods are changed.
This makes your programs easier to fix, update, and expand.
Practice
Solution
Step 1: Understand method overriding rules in Kotlin
In Kotlin, methods are final by default and cannot be overridden unless marked otherwise.Step 2: Identify the keyword to allow overriding
Theopenkeyword is used in the parent class method to allow it to be overridden in subclasses.Final Answer:
open -> Option AQuick Check:
Parent method must beopento override [OK]
- Using 'override' in parent method instead of 'open'
- Forgetting to mark parent method as 'open'
- Using 'final' which prevents overriding
greet in a Kotlin subclass?Solution
Step 1: Recall syntax for overriding methods in Kotlin
To override a method, the subclass method must use theoverridekeyword.Step 2: Check each option
override fun greet() { println("Hello") } correctly usesoverride fun greet(). fun greet() { println("Hello") } missesoverride. open fun greet() { println("Hello") } usesopenwhich is for parent methods. final fun greet() { println("Hello") } usesfinalwhich prevents overriding.Final Answer:
override fun greet() { println("Hello") } -> Option BQuick Check:
Useoverridekeyword in subclass method [OK]
- Omitting 'override' keyword in subclass method
- Using 'open' instead of 'override' in subclass
- Marking subclass method as 'final' which disallows override
open class Animal {
open fun sound() = "Generic sound"
}
class Dog : Animal() {
override fun sound() = "Bark"
}
fun main() {
val pet: Animal = Dog()
println(pet.sound())
}Solution
Step 1: Understand method overriding and polymorphism
The variablepetis of typeAnimalbut holds aDogobject. The overriddensound()method inDogwill be called.Step 2: Determine output of
Sincepet.sound()Dogoverridessound()to return "Bark", the output will be "Bark".Final Answer:
Bark -> Option CQuick Check:
Overridden method runs based on actual object type [OK]
- Thinking output is from parent class method
- Confusing variable type with object type
- Expecting compilation or runtime errors
open class Vehicle {
fun start() = "Starting"
}
class Car : Vehicle() {
override fun start() = "Car starting"
}Solution
Step 1: Check if parent method allows overriding
Thestart()method inVehicleis not markedopen, so it cannot be overridden.Step 2: Identify the error in subclass
TheCarclass tries to overridestart()but parent method is not open, causing a compilation error.Final Answer:
Missing 'open' keyword on Vehicle's start method -> Option DQuick Check:
Parent method must be 'open' to override [OK]
- Forgetting to mark parent method as 'open'
- Trying to override non-open method
- Assuming 'override' alone allows overriding
main() is run?
open class Printer {
open fun print() = "Printing document"
}
class ColorPrinter : Printer() {
override fun print() = "Printing in color"
}
class PhotoPrinter : ColorPrinter() {
override fun print() = "Printing photo in color"
}
fun main() {
val printer: Printer = PhotoPrinter()
println(printer.print())
}Solution
Step 1: Understand class hierarchy and method overriding
PhotoPrinteroverridesprint()fromColorPrinter, which overridesPrinter's method.Step 2: Determine which method runs for
The variableprinter.print()printeris of typePrinterbut holds aPhotoPrinterobject, so the most derived override runs, which isPhotoPrinter.print().Final Answer:
Printing photo in color -> Option AQuick Check:
Most derived override runs based on actual object type [OK]
- Thinking parent class method runs instead of child's
- Confusing override chain in multi-level inheritance
- Expecting compilation error due to multiple overrides
Ready to prove it?
Pass the test and this pattern moves unknown
