0
0
Kotlinprogramming~10 mins

Why delegation avoids inheritance in Kotlin - Visual Breakdown

Choose your learning style9 modes available
Concept Flow - Why delegation avoids inheritance
Start
Define Interface
Create Class A (implements Interface)
Create Class B (delegates to Class A)
Use Class B instance
Calls forwarded to Class A
Avoids inheritance issues
End
Shows how delegation forwards calls to another class instead of inheriting, avoiding tight coupling and allowing flexible behavior.
Execution Sample
Kotlin
interface Printer {
    fun print()
}

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

class DelegatingPrinter(printer: Printer) : Printer by printer

fun main() {
    val real = RealPrinter()
    val delegator = DelegatingPrinter(real)
    delegator.print()
}
This code shows a class delegating the print() call to another class instead of inheriting from it.
Execution Table
StepActionEvaluationResult
1Create RealPrinter instanceRealPrinter()real = RealPrinter instance
2Create DelegatingPrinter with realDelegatingPrinter(real)delegator delegates to real
3Call delegator.print()delegator.print()Call forwarded to real.print()
4real.print() executesprintln("Printing from RealPrinter")Output: Printing from RealPrinter
5End of mainNo more callsProgram ends
💡 Program ends after delegator.print() call is forwarded and executed
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3Final
realnullRealPrinter instanceRealPrinter instanceRealPrinter instanceRealPrinter instance
delegatornullnullDelegatingPrinter instance (delegates to real)DelegatingPrinter instanceDelegatingPrinter instance
Key Moments - 3 Insights
Why doesn't DelegatingPrinter inherit from RealPrinter?
DelegatingPrinter uses delegation to forward calls to RealPrinter instead of inheriting, so it avoids tight coupling and allows changing behavior by swapping the delegate (see execution_table step 2 and 3).
How does delegator.print() call reach RealPrinter's print()?
Delegation automatically forwards the print() call from DelegatingPrinter to the RealPrinter instance it holds (see execution_table step 3 and 4).
What problem does delegation solve compared to inheritance?
Delegation avoids the rigid class hierarchy and tight coupling of inheritance by forwarding calls to a contained object, allowing more flexible code reuse (see concept_flow and execution_table).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the value of 'delegator' after step 2?
ARealPrinter instance
BDelegatingPrinter instance delegating to real
Cnull
DPrinter interface
💡 Hint
Check variable_tracker row for 'delegator' after step 2
At which step does the actual print output happen?
AStep 2
BStep 3
CStep 4
DStep 5
💡 Hint
Look at execution_table where println is called
If DelegatingPrinter inherited from RealPrinter instead of delegating, what would change?
ADelegatingPrinter would be tightly coupled to RealPrinter
BDelegation would be more flexible
CCalls would still be forwarded automatically
DNo difference in behavior
💡 Hint
Refer to key_moments about inheritance vs delegation
Concept Snapshot
Delegation in Kotlin lets a class forward interface calls to another object.
Syntax: class B(printer: Printer) : Printer by printer
Avoids tight coupling of inheritance.
Allows flexible behavior by swapping delegate.
Calls to B are forwarded to the delegate object.
Useful to reuse code without subclassing.
Full Transcript
This example shows how delegation avoids inheritance in Kotlin. We define an interface Printer with a print() function. RealPrinter implements Printer and prints a message. DelegatingPrinter takes a Printer object and delegates all calls to it using 'by'. When we call print() on DelegatingPrinter, it forwards the call to RealPrinter's print(). This avoids inheritance's tight coupling and rigid hierarchy by forwarding calls to a contained object. The execution table traces creating instances and forwarding calls step-by-step. Variable tracker shows how variables change. Key moments clarify why delegation is preferred over inheritance here. The visual quiz tests understanding of delegation flow and differences from inheritance. The snapshot summarizes delegation syntax and benefits in Kotlin.