Challenge - 5 Problems
Delegation Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2: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() }
Attempts:
2 left
💡 Hint
Delegation forwards calls to the object it delegates to.
✗ Incorrect
The Delegator class delegates the print() call to the RealPrinter instance. So the output is from RealPrinter's print() method.
🧠 Conceptual
intermediate1:30remaining
Why delegation avoids tight coupling
Which reason best explains why delegation avoids tight coupling compared to inheritance?
Attempts:
2 left
💡 Hint
Think about flexibility in changing behavior without changing class hierarchy.
✗ Incorrect
Delegation lets an object forward tasks to another object that can be swapped or changed at runtime, reducing tight coupling.
🔧 Debug
advanced2: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() }
Attempts:
2 left
💡 Hint
Check which speak() method is called on robot.
✗ Incorrect
Robot defines its own speak() method which overrides the delegated one, so 'Robot speaking' is printed.
❓ Predict Output
advanced2: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() }
Attempts:
2 left
💡 Hint
Delegation is set at object creation and does not change when the delegate variable changes.
✗ Incorrect
Delegation in Kotlin is fixed at construction time. Changing the delegate variable later does not affect delegated calls.
🧠 Conceptual
expert2: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?
Attempts:
2 left
💡 Hint
Think about flexibility and combining behaviors.
✗ Incorrect
Delegation lets an object reuse code by forwarding calls to multiple delegate objects, avoiding the limitations of single inheritance.