0
0
Kotlinprogramming~10 mins

Overriding methods with override in Kotlin - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete 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'
Aopen
Boverride
Cfinal
Dabstract
Attempts:
3 left
💡 Hint
Common Mistakes
Forgetting to use 'override' keyword causes a compilation error.
Using 'open' instead of 'override' when overriding.
2fill in blank
medium

Complete 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'
Aoverride
Bfinal
Copen
Dabstract
Attempts:
3 left
💡 Hint
Common Mistakes
Not using 'override' keyword when overriding.
Trying to override a method without 'open' in superclass.
3fill in blank
hard

Fix 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'
Aopen
Bfinal
Cabstract
Doverride
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.
4fill in blank
hard

Fill 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'
Aoverride
Bsuper
Copen
Dfinal
Attempts:
3 left
💡 Hint
Common Mistakes
Forgetting 'override' keyword.
Not calling the superclass method with 'super'.
5fill in blank
hard

Fill 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'
Aoverride
Bsuper
C"Laptop ready to use"
D"Booting laptop"
Attempts:
3 left
💡 Hint
Common Mistakes
Not using 'override' keyword.
Forgetting to call the superclass method.
Using incorrect string message.