0
0
Kotlinprogramming~5 mins

Overriding methods with override in Kotlin - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What does the override keyword do in Kotlin?
It tells the compiler that a method in a subclass is replacing a method with the same name and parameters in its superclass.
Click to reveal answer
beginner
Why must you use override explicitly in Kotlin when overriding a method?
To avoid mistakes, Kotlin requires you to clearly say when you are changing a method from a parent class, making your code safer and easier to understand.
Click to reveal answer
beginner
What happens if you try to override a method without using override in Kotlin?
The compiler will give an error because Kotlin wants you to be clear about overriding methods.
Click to reveal answer
intermediate
Can you override a method that is not marked as open in Kotlin?
No, only methods marked with open or abstract methods can be overridden.
Click to reveal answer
beginner
Show a simple example of overriding a method with override in Kotlin.
open class Animal {
    open fun sound() = "Some sound"
}

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

fun main() {
    val dog = Dog()
    println(dog.sound()) // Output: Bark
}
Click to reveal answer
What keyword must you use in Kotlin to override a method from a superclass?
Aopen
Bsuper
Coverride
Dextends
Which keyword marks a method as overridable in Kotlin?
Aopen
Boverride
Cfinal
Dabstract
What happens if you override a method but forget to use override keyword?
AThe method overrides silently
BWarning only
CRuntime error
DCompiler error
Can you override a method marked as final in Kotlin?
ANo
BOnly if subclass is abstract
CYes
DOnly with <code>override</code>
In Kotlin, what is the purpose of overriding a method?
ATo add new methods to a class
BTo change the behavior of an inherited method
CTo delete a method from a class
DTo create a new class
Explain how and why you use the override keyword in Kotlin when working with classes.
Think about how Kotlin makes you clear when changing inherited methods.
You got /4 concepts.
    Describe the relationship between open and override keywords in Kotlin.
    One keyword allows change, the other confirms change.
    You got /4 concepts.