0
0
Kotlinprogramming~10 mins

Delegation vs inheritance decision in Kotlin - Interactive Practice

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

Complete the code to inherit from the base class.

Kotlin
open class Base {
    fun greet() = println("Hello from Base")
}

class Derived : [1]() {
}

fun main() {
    val obj = Derived()
    obj.greet()
}
Drag options to blanks, or click blank then click option'
ABase
BDerived
CAny
DObject
Attempts:
3 left
💡 Hint
Common Mistakes
Using the derived class name after the colon instead of the base class.
Forgetting the parentheses after the base class name.
2fill in blank
medium

Complete the code to delegate the interface implementation.

Kotlin
interface Printer {
    fun print()
}

class RealPrinter : Printer {
    override fun print() = println("Printing from RealPrinter")
}

class Delegator([1] printer: Printer) : Printer by printer

fun main() {
    val realPrinter = RealPrinter()
    val delegator = Delegator(realPrinter)
    delegator.print()
}
Drag options to blanks, or click blank then click option'
Aopen
Bprivate
Cval
Dvar
Attempts:
3 left
💡 Hint
Common Mistakes
Omitting val or var before the parameter.
Using private which is not allowed here.
3fill in blank
hard

Fix the error in the delegation syntax.

Kotlin
interface Speaker {
    fun speak()
}

class Person : Speaker {
    override fun speak() = println("Person speaking")
}

class Robot([1] speaker: Speaker) : Speaker by speaker

fun main() {
    val person = Person()
    val robot = Robot(person)
    robot.speak()
}
Drag options to blanks, or click blank then click option'
Avar
Bval
Cprivate
Dopen
Attempts:
3 left
💡 Hint
Common Mistakes
Using private which is not allowed for delegation parameters.
Omitting val or var.
4fill in blank
hard

Fill both blanks to create a delegated class that overrides a method.

Kotlin
interface Logger {
    fun log(message: String)
}

class ConsoleLogger : Logger {
    override fun log(message: String) = println("Console: $message")
}

class CustomLogger([1] logger: Logger) : Logger by logger {
    override fun log(message: String) {
        println("Custom log start")
        logger.[2](message)
        println("Custom log end")
    }
}

fun main() {
    val consoleLogger = ConsoleLogger()
    val customLogger = CustomLogger(consoleLogger)
    customLogger.log("Hello")
}
Drag options to blanks, or click blank then click option'
Aval
Bvar
Clog
Dprint
Attempts:
3 left
💡 Hint
Common Mistakes
Using var instead of val (both work but val is preferred).
Calling print instead of log.
5fill in blank
hard

Fill both blanks to create a delegated class that adds behavior before and after delegation.

Kotlin
interface Notifier {
    fun notify(message: String)
}

class EmailNotifier : Notifier {
    override fun notify(message: String) = println("Email: $message")
}

class AdvancedNotifier([1] notifier: Notifier) : Notifier by notifier {
    override fun notify(message: String) {
        println("Start notification")
        notifier.[2](message)
        println("End notification")
    }

    fun extra() = println("Extra functionality")
}

fun main() {
    val emailNotifier = EmailNotifier()
    val advNotifier = AdvancedNotifier(emailNotifier)
    advNotifier.notify("Meeting at 10")
}
Drag options to blanks, or click blank then click option'
Aval
Bnotify
Cprint
Dvar
Attempts:
3 left
💡 Hint
Common Mistakes
Using print instead of notify.
Omitting val before the constructor parameter.