0
0
Kotlinprogramming~20 mins

Overriding methods with override in Kotlin - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Override Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of overridden method call
What is the output of this Kotlin code when calling greet() on an instance of Dog?
Kotlin
open class Animal {
    open fun greet() = "Hello from Animal"
}

class Dog : Animal() {
    override fun greet() = "Woof from Dog"
}

fun main() {
    val pet: Animal = Dog()
    println(pet.greet())
}
AWoof from Dog
BHello from Animal
CCompilation error: missing override keyword
DRuntime error: method not found
Attempts:
2 left
💡 Hint
Remember that the override keyword allows a subclass to provide its own implementation of a method.
Predict Output
intermediate
2:00remaining
Output when calling base and overridden methods
What will be printed when running this Kotlin code?
Kotlin
open class Vehicle {
    open fun start() = "Vehicle started"
}

class Car : Vehicle() {
    override fun start() = "Car started"
    fun startBase() = super.start()
}

fun main() {
    val myCar = Car()
    println(myCar.start())
    println(myCar.startBase())
}
ACompilation error: cannot call super in this context
BCar started\nCar started
CVehicle started\nVehicle started
DCar started\nVehicle started
Attempts:
2 left
💡 Hint
The super keyword calls the base class method even if it is overridden.
🔧 Debug
advanced
2:00remaining
Identify the error in method overriding
Why does this Kotlin code fail to compile?
Kotlin
open class Shape {
    fun draw() = "Drawing shape"
}

class Circle : Shape() {
    override fun draw() = "Drawing circle"
}
ACannot override 'draw' because it is not open in base class
BMissing override keyword in subclass
CFunction signatures do not match
DNo error, code compiles fine
Attempts:
2 left
💡 Hint
In Kotlin, only methods marked open can be overridden.
📝 Syntax
advanced
2:00remaining
Correct override syntax
Which option correctly overrides the calculate() method in Kotlin?
Kotlin
open class Calculator {
    open fun calculate(x: Int): Int = x * 2
}

class AdvancedCalculator : Calculator() {
    // override method here
}
Aoverride calculate(x: Int): Int = x * 3
Bfun override calculate(x: Int): Int = x * 3
Coverride fun calculate(x: Int): Int = x * 3
Doverride fun calculate(x): Int = x * 3
Attempts:
2 left
💡 Hint
The override keyword must come before fun and the parameter types must be specified.
🚀 Application
expert
3:00remaining
Determine output with multiple overrides and calls
What is the output of this Kotlin program?
Kotlin
open class A {
    open fun foo() = "A"
    fun bar() = foo()
}

class B : A() {
    override fun foo() = "B"
}

class C : B() {
    override fun foo() = "C"
    fun test() = super.foo()
}

fun main() {
    val c = C()
    println(c.foo())
    println(c.bar())
    println(c.test())
}
AC\nB\nB
BC\nC\nB
CB\nB\nA
DC\nA\nB
Attempts:
2 left
💡 Hint
Remember that super refers to the immediate superclass, and method calls use the most derived override.