0
0
Kotlinprogramming~10 mins

Class delegation with by keyword in Kotlin - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Class delegation with by keyword
Define Interface
Create Delegate Class
Create Delegating Class with 'by'
Call Method on Delegating Class
Method Call Forwarded to Delegate
Delegate Executes Method
The delegating class forwards calls to the delegate class using 'by', so it reuses delegate's behavior.
Execution Sample
Kotlin
interface Printer {
    fun print()
}

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

class DelegatingPrinter(printer: Printer) : Printer by printer

fun main() {
    val printer = DelegatingPrinter(ConsolePrinter())
    printer.print()
}
This code shows a class delegating the print() method to another class using 'by'.
Execution Table
StepActionObjectMethod CalledDelegate UsedOutput
1Create ConsolePrinter instanceConsolePrinter---
2Create DelegatingPrinter with ConsolePrinterDelegatingPrinter-ConsolePrinter-
3Call print() on DelegatingPrinterDelegatingPrinterprint()ConsolePrinter-
4DelegatingPrinter forwards print() to ConsolePrinterConsolePrinterprint()-Printing to console
5print() method completesConsolePrinter---
6Program ends----
💡 All method calls delegated; program ends after print() output.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3Final
printernullnullDelegatingPrinter(ConsolePrinter())DelegatingPrinter(ConsolePrinter())DelegatingPrinter(ConsolePrinter())
Key Moments - 2 Insights
Why does calling print() on DelegatingPrinter actually run ConsolePrinter's print()?
Because DelegatingPrinter uses 'by printer' to delegate all Printer interface calls to the ConsolePrinter instance, as shown in execution_table step 4.
Does DelegatingPrinter need to implement print() method explicitly?
No, the 'by' keyword automatically forwards calls to the delegate, so no explicit implementation is needed (see execution_table steps 3 and 4).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, at which step is the print() method actually executed?
AStep 3
BStep 4
CStep 2
DStep 1
💡 Hint
Check the 'Output' column in execution_table rows to see when 'Printing to console' appears.
According to variable_tracker, what does 'printer' hold after step 2?
ADelegatingPrinter(ConsolePrinter())
Bnull
CConsolePrinter instance
Dprint() method
💡 Hint
Look at the 'After Step 2' column for 'printer' in variable_tracker.
If DelegatingPrinter did not use 'by', what would happen when calling print()?
AIt would call ConsolePrinter's print() anyway
BIt would print twice
CIt would cause a compile error unless print() is implemented
DIt would do nothing silently
💡 Hint
Recall that 'by' keyword delegates method calls; without it, methods must be implemented explicitly.
Concept Snapshot
interface Printer { fun print() }
class ConsolePrinter : Printer { override fun print() = println("...") }
class DelegatingPrinter(printer: Printer) : Printer by printer

'by' keyword forwards interface calls to delegate instance automatically.
Full Transcript
Class delegation in Kotlin lets one class reuse another's behavior by forwarding interface method calls using the 'by' keyword. Here, DelegatingPrinter implements Printer by delegating to ConsolePrinter. When print() is called on DelegatingPrinter, it forwards the call to ConsolePrinter's print(), which prints to the console. This avoids writing boilerplate code. The execution table shows object creation, method calls, delegation, and output. Variable tracker shows how the 'printer' variable holds the delegate instance. Key moments clarify why delegation works and that explicit method implementation is not needed. The quiz tests understanding of delegation steps and effects. This is a simple way to share behavior between classes in Kotlin.