0
0
Kotlinprogramming~10 mins

Class delegation with by keyword in Kotlin - Interactive Code Practice

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

Complete the code to delegate the interface implementation using the 'by' keyword.

Kotlin
interface Printer {
    fun printMessage()
}

class ConsolePrinter : Printer {
    override fun printMessage() {
        println("Printing to console")
    }
}

class Delegator(private val printer: Printer) : Printer [1] printer

fun main() {
    val printer = ConsolePrinter()
    val delegator = Delegator(printer)
    delegator.printMessage()
}
Drag options to blanks, or click blank then click option'
Aimplements
Bextends
Cby
D:
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'implements' or 'extends' which are Java keywords, not Kotlin delegation keywords.
Using ':' alone without 'by' does not delegate implementation.
2fill in blank
medium

Complete the code to delegate the 'calculate' function to the CalculatorImpl class.

Kotlin
interface Calculator {
    fun calculate(x: Int, y: Int): Int
}

class CalculatorImpl : Calculator {
    override fun calculate(x: Int, y: Int): Int {
        return x + y
    }
}

class DelegatedCalculator(private val calc: Calculator) : Calculator [1] calc

fun main() {
    val calculator = CalculatorImpl()
    val delegated = DelegatedCalculator(calculator)
    println(delegated.calculate(5, 3))
}
Drag options to blanks, or click blank then click option'
Aimplements
Bby
Cextends
D:
Attempts:
3 left
💡 Hint
Common Mistakes
Using Java keywords like 'implements' or 'extends' instead of Kotlin's 'by'.
Omitting the delegation keyword causes a compilation error.
3fill in blank
hard

Fix the error in the delegation syntax to correctly delegate the 'speak' function.

Kotlin
interface Speaker {
    fun speak()
}

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

class Person(private val speaker: Speaker) : Speaker [1] speaker

fun main() {
    val englishSpeaker = EnglishSpeaker()
    val person = Person(englishSpeaker)
    person.speak()
}
Drag options to blanks, or click blank then click option'
Aby
Bimplements
Cextends
D:
Attempts:
3 left
💡 Hint
Common Mistakes
Using Java keywords like 'implements' or 'extends' instead of 'by'.
Using ':' alone without 'by' causes syntax errors.
4fill in blank
hard

Fill both blanks to delegate the 'draw' function to the 'ShapeImpl' class.

Kotlin
interface Shape {
    fun draw()
}

class ShapeImpl : Shape {
    override fun draw() {
        println("Drawing shape")
    }
}

class ShapeDelegate(private val shape: Shape) : Shape [1] [2]

fun main() {
    val shapeImpl = ShapeImpl()
    val shapeDelegate = ShapeDelegate(shapeImpl)
    shapeDelegate.draw()
}
Drag options to blanks, or click blank then click option'
Aby
Bimplements
Cshape
Dextends
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'implements' or 'extends' instead of 'by'.
Not specifying the object to delegate to.
5fill in blank
hard

Fill all three blanks to delegate the 'play' function to the 'MusicPlayerImpl' class and override it in the delegator.

Kotlin
interface MusicPlayer {
    fun play()
}

class MusicPlayerImpl : MusicPlayer {
    override fun play() {
        println("Playing music")
    }
}

class CustomPlayer(private val player: MusicPlayer) : MusicPlayer [1] [2] {
    override fun play() {
        println("Custom player start")
        player.[3]()
        println("Custom player end")
    }
}

fun main() {
    val impl = MusicPlayerImpl()
    val custom = CustomPlayer(impl)
    custom.play()
}
Drag options to blanks, or click blank then click option'
Aby
Bplayer
Cplay
Dimplements
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'implements' instead of 'by'.
Forgetting to call the delegated function inside the override.
Using wrong variable or function names.