Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to correctly override the method in Kotlin.
Kotlin
open class Animal { open fun sound() { println("Animal sound") } } class Dog : Animal() { [1] fun sound() { println("Bark") } }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Forgetting to use 'override' keyword causes a compilation error.
Using 'open' instead of 'override' when overriding.
✗ Incorrect
In Kotlin, to override a method from a superclass, you must use the 'override' keyword before the function.
2fill in blank
mediumComplete the code to override the method and call the superclass method inside it.
Kotlin
open class Vehicle { open fun start() { println("Vehicle started") } } class Car : Vehicle() { [1] fun start() { super.start() println("Car started") } }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Not using 'override' keyword when overriding.
Trying to override a method without 'open' in superclass.
✗ Incorrect
The 'override' keyword is needed to override a method, and you can call the superclass method using 'super.methodName()'.
3fill in blank
hardFix the error by correctly overriding the method in the subclass.
Kotlin
open class Printer { open fun printMessage() { println("Printing message") } } class ColorPrinter : Printer() { [1] fun printMessage() { println("Printing in color") } }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Omitting the 'override' keyword causes the method to be treated as a new method.
Trying to override without 'open' in superclass method.
✗ Incorrect
To override the method from the superclass, the subclass method must have the 'override' keyword.
4fill in blank
hardFill both blanks to override the method and call the superclass method inside it.
Kotlin
open class Appliance { open fun turnOn() { println("Appliance is on") } } class Fan : Appliance() { [1] fun turnOn() { [2].turnOn() println("Fan is spinning") } }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Forgetting 'override' keyword.
Not calling the superclass method with 'super'.
✗ Incorrect
Use 'override' to override the method and 'super' to call the superclass method.
5fill in blank
hardFill all three blanks to override a method, call the superclass method, and add extra behavior.
Kotlin
open class Computer { open fun boot() { println("Computer booting") } } class Laptop : Computer() { [1] fun boot() { [2].boot() println([3]) } }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Not using 'override' keyword.
Forgetting to call the superclass method.
Using incorrect string message.
✗ Incorrect
Use 'override' to override, 'super' to call the superclass method, and add a custom message string.