Bird
Raised Fist0
Kotlinprogramming~10 mins

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

Choose your learning style10 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.

Practice

(1/5)
1. What keyword must be used in Kotlin to allow a method in a parent class to be overridden in a child class?
easy
A. open
B. override
C. final
D. abstract

Solution

  1. Step 1: Understand method overriding rules in Kotlin

    In Kotlin, methods are final by default and cannot be overridden unless marked otherwise.
  2. Step 2: Identify the keyword to allow overriding

    The open keyword is used in the parent class method to allow it to be overridden in subclasses.
  3. Final Answer:

    open -> Option A
  4. Quick Check:

    Parent method must be open to override [OK]
Hint: Parent method needs 'open' to allow override [OK]
Common Mistakes:
  • Using 'override' in parent method instead of 'open'
  • Forgetting to mark parent method as 'open'
  • Using 'final' which prevents overriding
2. Which of the following is the correct way to override a method named greet in a Kotlin subclass?
easy
A. fun greet() { println("Hello") }
B. override fun greet() { println("Hello") }
C. open fun greet() { println("Hello") }
D. final fun greet() { println("Hello") }

Solution

  1. Step 1: Recall syntax for overriding methods in Kotlin

    To override a method, the subclass method must use the override keyword.
  2. Step 2: Check each option

    override fun greet() { println("Hello") } correctly uses override fun greet(). fun greet() { println("Hello") } misses override. open fun greet() { println("Hello") } uses open which is for parent methods. final fun greet() { println("Hello") } uses final which prevents overriding.
  3. Final Answer:

    override fun greet() { println("Hello") } -> Option B
  4. Quick Check:

    Use override keyword in subclass method [OK]
Hint: Use 'override' keyword before method in subclass [OK]
Common Mistakes:
  • Omitting 'override' keyword in subclass method
  • Using 'open' instead of 'override' in subclass
  • Marking subclass method as 'final' which disallows override
3. What will be the output of the following Kotlin code?
open class Animal {
    open fun sound() = "Generic sound"
}

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

fun main() {
    val pet: Animal = Dog()
    println(pet.sound())
}
medium
A. Generic sound
B. Compilation error
C. Bark
D. Runtime exception

Solution

  1. Step 1: Understand method overriding and polymorphism

    The variable pet is of type Animal but holds a Dog object. The overridden sound() method in Dog will be called.
  2. Step 2: Determine output of pet.sound()

    Since Dog overrides sound() to return "Bark", the output will be "Bark".
  3. Final Answer:

    Bark -> Option C
  4. Quick Check:

    Overridden method runs based on actual object type [OK]
Hint: Overridden method runs from actual object type [OK]
Common Mistakes:
  • Thinking output is from parent class method
  • Confusing variable type with object type
  • Expecting compilation or runtime errors
4. Identify the error in the following Kotlin code:
open class Vehicle {
    fun start() = "Starting"
}

class Car : Vehicle() {
    override fun start() = "Car starting"
}
medium
A. No error, code is correct
B. Missing 'override' keyword in Car's start method
C. Cannot override a method in Kotlin
D. Missing 'open' keyword on Vehicle's start method

Solution

  1. Step 1: Check if parent method allows overriding

    The start() method in Vehicle is not marked open, so it cannot be overridden.
  2. Step 2: Identify the error in subclass

    The Car class tries to override start() but parent method is not open, causing a compilation error.
  3. Final Answer:

    Missing 'open' keyword on Vehicle's start method -> Option D
  4. Quick Check:

    Parent method must be 'open' to override [OK]
Hint: Parent method must be 'open' to override it [OK]
Common Mistakes:
  • Forgetting to mark parent method as 'open'
  • Trying to override non-open method
  • Assuming 'override' alone allows overriding
5. Given the following Kotlin classes, what will be the output when 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())
}
hard
A. Printing photo in color
B. Printing in color
C. Printing document
D. Compilation error due to multiple overrides

Solution

  1. Step 1: Understand class hierarchy and method overriding

    PhotoPrinter overrides print() from ColorPrinter, which overrides Printer's method.
  2. Step 2: Determine which method runs for printer.print()

    The variable printer is of type Printer but holds a PhotoPrinter object, so the most derived override runs, which is PhotoPrinter.print().
  3. Final Answer:

    Printing photo in color -> Option A
  4. Quick Check:

    Most derived override runs based on actual object type [OK]
Hint: Most derived override runs, even with parent type variable [OK]
Common Mistakes:
  • Thinking parent class method runs instead of child's
  • Confusing override chain in multi-level inheritance
  • Expecting compilation error due to multiple overrides
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard