0
0
Kotlinprogramming~20 mins

Why delegation avoids inheritance in Kotlin - Challenge Your Understanding

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Delegation Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of delegation vs inheritance example
What is the output of this Kotlin code that uses delegation instead of inheritance?
Kotlin
interface Printer {
    fun print()
}

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

class Delegator(printer: Printer) : Printer by printer

fun main() {
    val realPrinter = RealPrinter()
    val delegator = Delegator(realPrinter)
    delegator.print()
}
ARuntime error: NullPointerException
BPrinting from Delegator
CCompilation error due to delegation
DPrinting from RealPrinter
Attempts:
2 left
💡 Hint
Delegation forwards calls to the object it delegates to.
🧠 Conceptual
intermediate
1:30remaining
Why delegation avoids tight coupling
Which reason best explains why delegation avoids tight coupling compared to inheritance?
ADelegation allows changing behavior at runtime by swapping delegate objects.
BDelegation forces all subclasses to share the same implementation.
CDelegation requires the subclass to override all methods explicitly.
DDelegation prevents code reuse across classes.
Attempts:
2 left
💡 Hint
Think about flexibility in changing behavior without changing class hierarchy.
🔧 Debug
advanced
2:00remaining
Identify the error in delegation usage
What error will this Kotlin code produce and why?
Kotlin
interface Speaker {
    fun speak()
}

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

class Robot(speaker: Speaker) : Speaker by speaker {
    fun speak() {
        println("Robot speaking")
    }
}

fun main() {
    val person = Person()
    val robot = Robot(person)
    robot.speak()
}
APrints 'Hello'
BPrints 'Robot speaking'
CCompilation error: 'speak' overrides nothing
DRuntime error: StackOverflowError
Attempts:
2 left
💡 Hint
Check which speak() method is called on robot.
Predict Output
advanced
2:00remaining
Output when delegation changes delegate at runtime
What is the output of this Kotlin code that changes the delegate object at runtime?
Kotlin
interface Worker {
    fun work()
}

class Developer : Worker {
    override fun work() {
        println("Writing code")
    }
}

class Tester : Worker {
    override fun work() {
        println("Testing code")
    }
}

class Manager(var worker: Worker) : Worker by worker {
    fun changeWorker(newWorker: Worker) {
        worker = newWorker
    }
}

fun main() {
    val dev = Developer()
    val test = Tester()
    val manager = Manager(dev)
    manager.work()
    manager.changeWorker(test)
    manager.work()
}
A
Writing code
Testing code
BCompilation error: delegation cannot be changed
C
Writing code
Writing code
D
Testing code
Testing code
Attempts:
2 left
💡 Hint
Delegation is set at object creation and does not change when the delegate variable changes.
🧠 Conceptual
expert
2:30remaining
Why delegation is preferred over inheritance for code reuse
Which statement best explains why delegation is often preferred over inheritance for code reuse in Kotlin?
ADelegation allows combining behaviors from multiple sources without a rigid class hierarchy.
BDelegation forces all subclasses to inherit from a single parent class.
CDelegation prevents any form of polymorphism.
DDelegation requires less code but cannot be used with interfaces.
Attempts:
2 left
💡 Hint
Think about flexibility and combining behaviors.