0
0
Kotlinprogramming~10 mins

Open classes and methods 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 make the class extendable.

Kotlin
open class Vehicle {
    fun drive() {
        println("Driving")
    }
}

class Car : [1] Vehicle() {}
Drag options to blanks, or click blank then click option'
Aabstract
Bopen
Cfinal
Dsealed
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'final' which prevents inheritance.
Forgetting to mark the class as 'open'.
2fill in blank
medium

Complete the code to allow the method to be overridden.

Kotlin
open class Animal {
    [1] fun sound() {
        println("Some sound")
    }
}
Drag options to blanks, or click blank then click option'
Aopen
Bfinal
Coverride
Dabstract
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'final' which prevents overriding.
Using 'override' keyword in the base class.
3fill in blank
hard

Fix the error in the subclass method overriding.

Kotlin
open class Bird {
    open fun fly() {
        println("Flying")
    }
}

class Eagle : Bird() {
    override fun [1]() {
        println("Eagle flying")
    }
}
Drag options to blanks, or click blank then click option'
Aswim
Brun
Cfly
Djump
Attempts:
3 left
💡 Hint
Common Mistakes
Using a different method name in the override.
Forgetting to mark the base method as open.
4fill in blank
hard

Fill both blanks to create an open class with an open method.

Kotlin
[1] class Shape {
    [2] fun draw() {
        println("Drawing shape")
    }
}
Drag options to blanks, or click blank then click option'
Aopen
Bfinal
Dabstract
Attempts:
3 left
💡 Hint
Common Mistakes
Marking class as open but method as final.
Forgetting to mark either class or method as open.
5fill in blank
hard

Fill all three blanks to override an open method in a subclass.

Kotlin
open class Printer {
    open fun [1]() {
        println("Printing document")
    }
}

class ColorPrinter : Printer() {
    override fun [2]() {
        println([3])
    }
}
Drag options to blanks, or click blank then click option'
Aprint
C"Printing in color"
D"Printing in black and white"
Attempts:
3 left
💡 Hint
Common Mistakes
Using different method names in override.
Printing wrong message in subclass.