Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'final' which prevents inheritance.
Forgetting to mark the class as 'open'.
✗ Incorrect
In Kotlin, to allow a class to be inherited, it must be declared as open.
2fill in blank
mediumComplete 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'final' which prevents overriding.
Using 'override' keyword in the base class.
✗ Incorrect
Methods must be marked open to be overridden in subclasses.
3fill in blank
hardFix 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using a different method name in the override.
Forgetting to mark the base method as open.
✗ Incorrect
The method name in the subclass must match the open method in the superclass to override it.
4fill in blank
hardFill 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Marking class as open but method as final.
Forgetting to mark either class or method as open.
✗ Incorrect
Both the class and the method must be marked open to allow inheritance and overriding.
5fill in blank
hardFill 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using different method names in override.
Printing wrong message in subclass.
✗ Incorrect
The method name must match in both classes, and the subclass method can provide a different implementation.