0
0
Kotlinprogramming~10 mins

Overriding methods with override in Kotlin - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Overriding methods with override
Define base class with open method
Define subclass with override method
Create subclass instance
Call method on subclass instance
Subclass method runs, overriding base method
Output result from subclass method
This flow shows how a subclass replaces a base class method using override, so calling the method runs the subclass version.
Execution Sample
Kotlin
open class Animal {
    open fun sound() = "Some sound"
}

class Dog : Animal() {
    override fun sound() = "Bark"
}

fun main() {
    val dog = Dog()
    println(dog.sound())
}
This code defines a base class Animal with a method sound, then a subclass Dog overrides sound. Calling dog.sound() prints "Bark".
Execution Table
StepActionEvaluationResult
1Define class Animal with open method soundsound() returns "Some sound"Animal class ready
2Define class Dog overriding soundsound() returns "Bark"Dog class ready
3Create instance dog of Dogdog is Dog objectdog created
4Call dog.sound()Calls Dog's override sound()Returns "Bark"
5Print dog.sound() resultOutput to consoleBark
💡 Program ends after printing overridden method output
Variable Tracker
VariableStartAfter Step 3Final
dogundefinedDog instance createdDog instance with overridden sound()
Key Moments - 3 Insights
Why do we need the 'override' keyword in the subclass method?
The 'override' keyword tells Kotlin this method replaces the base class method. Without it, the compiler gives an error (see step 2 in execution_table).
What happens if the base method is not marked 'open'?
If the base method is not 'open', you cannot override it in the subclass. Kotlin will give a compile error before step 2.
When calling dog.sound(), why does it run the Dog version, not Animal?
Because Dog overrides sound(), the call uses Dog's method (step 4), showing polymorphism.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what does dog.sound() return at step 4?
AError
B"Some sound"
C"Bark"
Dnull
💡 Hint
Check step 4 in execution_table where dog.sound() calls Dog's override method
At which step is the Dog instance created?
AStep 3
BStep 1
CStep 2
DStep 5
💡 Hint
Look at variable_tracker and execution_table step 3 for instance creation
If the base method sound() was not marked 'open', what would happen?
ADog can still override without error
BCompile error at step 2
CDog's sound() runs but base sound() ignored
DProgram runs but prints base sound()
💡 Hint
Refer to key_moments about 'open' keyword importance for overriding
Concept Snapshot
open class Base {
  open fun method() { }
}

class Sub : Base() {
  override fun method() { }
}

- 'open' allows method to be overridden
- 'override' replaces base method
- Calls on subclass use overridden method
- Compiler enforces override keyword
Full Transcript
This example shows how Kotlin uses 'open' and 'override' keywords to allow a subclass to replace a method from its base class. The base class Animal has an open method sound() returning "Some sound". The subclass Dog overrides sound() with 'override' keyword to return "Bark". When we create a Dog instance and call dog.sound(), the overridden Dog method runs, printing "Bark". The execution table traces defining classes, creating the instance, calling the method, and printing the output. Key points include the need for 'open' in the base method and 'override' in the subclass method. The visual quiz tests understanding of method return values, instance creation, and the role of 'open'. This helps beginners see how method overriding works step-by-step in Kotlin.