0
0
Kotlinprogramming~10 mins

Why delegation avoids inheritance in Kotlin - Test Your Understanding

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

Complete the code to delegate the print function to the printer object.

Kotlin
class Printer {
    fun print() {
        println("Printing document")
    }
}

class Document(val printer: Printer) {
    fun print() = printer.[1]()
}
Drag options to blanks, or click blank then click option'
AprintDocument
Bprint
Cprintln
Ddisplay
Attempts:
3 left
💡 Hint
Common Mistakes
Using println instead of print causes a compilation error.
Calling a non-existent method like printDocument or display.
2fill in blank
medium

Complete the code to use Kotlin's delegation syntax for the interface Printer.

Kotlin
interface Printer {
    fun print()
}

class RealPrinter : Printer {
    override fun print() {
        println("Real printing")
    }
}

class Document(printer: Printer) : Printer by [1] {
}

fun main() {
    val doc = Document(RealPrinter())
    doc.print()
}
Drag options to blanks, or click blank then click option'
Aprinter
BRealPrinter()
Cthis
DDocument()
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'RealPrinter()' directly instead of the parameter name.
Using 'this' or 'Document()' which are incorrect here.
3fill in blank
hard

Fix the error by completing the code to delegate the interface correctly.

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

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

class Service : Logger by [1] {
}

fun main() {
    val service = Service()
    service.log("Started")
}
Drag options to blanks, or click blank then click option'
Athis
BConsoleLogger
CConsoleLogger()
DLogger()
Attempts:
3 left
💡 Hint
Common Mistakes
Using the class name without parentheses causes an error.
Using 'this' or 'Logger()' which are invalid here.
4fill in blank
hard

Fill both blanks to create a class that delegates Logger and adds a new function.

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

class FileLogger : Logger {
    override fun log(message: String) {
        println("File log: $message")
    }
}

class AppLogger([1]: Logger) : Logger by [2] {
    fun error(message: String) {
        log("Error: $message")
    }
}
Drag options to blanks, or click blank then click option'
Alogger
Blog
Attempts:
3 left
💡 Hint
Common Mistakes
Using different names for the parameter and delegation causes errors.
Using 'log' which is a function name, not a variable.
5fill in blank
hard

Fill all three blanks to implement delegation with an additional method in Kotlin.

Kotlin
interface Speaker {
    fun speak()
}

class Human : Speaker {
    override fun speak() {
        println("Hello!")
    }
}

class Robot([1]: Speaker) : Speaker by [2] {
    fun compute() {
        println("Computing...")
    }
}

fun main() {
    val robot = Robot([3])
    robot.speak()
    robot.compute()
}
Drag options to blanks, or click blank then click option'
Ahuman
CHuman()
Drobot
Attempts:
3 left
💡 Hint
Common Mistakes
Using different names for parameter and delegation.
Passing 'robot' instead of 'Human()' when creating Robot.