Bird
Raised Fist0
Kotlinprogramming~10 mins

Delegation vs inheritance decision in Kotlin - Interactive Practice

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

Complete the code to inherit from the base class.

Kotlin
open class Base {
    fun greet() = println("Hello from Base")
}

class Derived : [1]() {
}

fun main() {
    val obj = Derived()
    obj.greet()
}
Drag options to blanks, or click blank then click option'
ABase
BDerived
CAny
DObject
Attempts:
3 left
💡 Hint
Common Mistakes
Using the derived class name after the colon instead of the base class.
Forgetting the parentheses after the base class name.
2fill in blank
medium

Complete the code to delegate the interface implementation.

Kotlin
interface Printer {
    fun print()
}

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

class Delegator([1] printer: Printer) : Printer by printer

fun main() {
    val realPrinter = RealPrinter()
    val delegator = Delegator(realPrinter)
    delegator.print()
}
Drag options to blanks, or click blank then click option'
Aopen
Bprivate
Cval
Dvar
Attempts:
3 left
💡 Hint
Common Mistakes
Omitting val or var before the parameter.
Using private which is not allowed here.
3fill in blank
hard

Fix the error in the delegation syntax.

Kotlin
interface Speaker {
    fun speak()
}

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

class Robot([1] speaker: Speaker) : Speaker by speaker

fun main() {
    val person = Person()
    val robot = Robot(person)
    robot.speak()
}
Drag options to blanks, or click blank then click option'
Avar
Bval
Cprivate
Dopen
Attempts:
3 left
💡 Hint
Common Mistakes
Using private which is not allowed for delegation parameters.
Omitting val or var.
4fill in blank
hard

Fill both blanks to create a delegated class that overrides a method.

Kotlin
interface Logger {
    fun log(message: String)
}

class ConsoleLogger : Logger {
    override fun log(message: String) = println("Console: $message")
}

class CustomLogger([1] logger: Logger) : Logger by logger {
    override fun log(message: String) {
        println("Custom log start")
        logger.[2](message)
        println("Custom log end")
    }
}

fun main() {
    val consoleLogger = ConsoleLogger()
    val customLogger = CustomLogger(consoleLogger)
    customLogger.log("Hello")
}
Drag options to blanks, or click blank then click option'
Aval
Bvar
Clog
Dprint
Attempts:
3 left
💡 Hint
Common Mistakes
Using var instead of val (both work but val is preferred).
Calling print instead of log.
5fill in blank
hard

Fill both blanks to create a delegated class that adds behavior before and after delegation.

Kotlin
interface Notifier {
    fun notify(message: String)
}

class EmailNotifier : Notifier {
    override fun notify(message: String) = println("Email: $message")
}

class AdvancedNotifier([1] notifier: Notifier) : Notifier by notifier {
    override fun notify(message: String) {
        println("Start notification")
        notifier.[2](message)
        println("End notification")
    }

    fun extra() = println("Extra functionality")
}

fun main() {
    val emailNotifier = EmailNotifier()
    val advNotifier = AdvancedNotifier(emailNotifier)
    advNotifier.notify("Meeting at 10")
}
Drag options to blanks, or click blank then click option'
Aval
Bnotify
Cprint
Dvar
Attempts:
3 left
💡 Hint
Common Mistakes
Using print instead of notify.
Omitting val before the constructor parameter.

Practice

(1/5)
1. Which statement best describes when to use inheritance in Kotlin?
easy
A. When you want to hide implementation details completely.
B. When you want to reuse code without creating a parent-child relationship.
C. When a class is a specialized type of another class ("is-a" relationship).
D. When you want to avoid any relationship between classes.

Solution

  1. Step 1: Understand inheritance purpose

    Inheritance models an "is-a" relationship where a subclass extends a superclass.
  2. Step 2: Match the correct use case

    Using inheritance is appropriate when a class is a specialized version of another class.
  3. Final Answer:

    When a class is a specialized type of another class ("is-a" relationship). -> Option C
  4. Quick Check:

    Inheritance = "is-a" relationship [OK]
Hint: Use inheritance for clear "is-a" relationships [OK]
Common Mistakes:
  • Confusing delegation with inheritance
  • Using inheritance for code reuse only
  • Thinking inheritance hides implementation
2. Which Kotlin syntax correctly shows delegation of interface Printer by class Document?
easy
A. class Document : Printer(printer)
B. class Document(val printer: Printer) : Printer by printer
C. class Document : Printer implements printer
D. class Document(printer: Printer) extends Printer

Solution

  1. Step 1: Recall Kotlin delegation syntax

    Kotlin uses : Interface by instance to delegate interface implementation.
  2. Step 2: Identify correct syntax

    class Document(val printer: Printer) : Printer by printer correctly shows delegation with class Document(val printer: Printer) : Printer by printer.
  3. Final Answer:

    class Document(val printer: Printer) : Printer by printer -> Option B
  4. Quick Check:

    Delegation syntax = : Interface by instance [OK]
Hint: Delegation uses ': Interface by instance' syntax [OK]
Common Mistakes:
  • Using extends keyword (not Kotlin)
  • Writing implements instead of by
  • Missing val/var for delegated instance
3. What is the output of this Kotlin code?
interface Logger {
    fun log(message: String)
}

class ConsoleLogger : Logger {
    override fun log(message: String) {
        println("Console: $message")
    }
}

class FileLogger : Logger {
    override fun log(message: String) {
        println("File: $message")
    }
}

class Service(logger: Logger) : Logger by logger

fun main() {
    val service = Service(ConsoleLogger())
    service.log("Start")
}
medium
A. Console: Start
B. File: Start
C. Start
D. Compilation error

Solution

  1. Step 1: Understand delegation in Service class

    Service delegates Logger interface to the passed logger instance, here ConsoleLogger.
  2. Step 2: Trace the log call

    Calling service.log("Start") calls ConsoleLogger.log, printing "Console: Start".
  3. Final Answer:

    Console: Start -> Option A
  4. Quick Check:

    Delegation calls ConsoleLogger.log = Console: Start [OK]
Hint: Delegation calls passed instance methods [OK]
Common Mistakes:
  • Confusing which logger is used
  • Expecting FileLogger output
  • Thinking delegation copies methods
4. Identify the error in this Kotlin code using delegation:
interface Worker {
    fun work()
}

class Employee : Worker {
    override fun work() {
        println("Employee working")
    }
}

class Manager(worker: Worker) : Worker by worker {
    fun manage() {
        println("Managing team")
    }
}

fun main() {
    val manager = Manager()
    manager.work()
    manager.manage()
}
medium
A. Manager class missing argument for Worker in constructor call.
B. Employee class does not implement work() method.
C. Delegation syntax is incorrect; should use extends instead of by.
D. manage() method cannot be called on Manager instance.

Solution

  1. Step 1: Check Manager constructor usage

    Manager requires a Worker parameter in constructor but none is provided in main().
  2. Step 2: Identify compilation error

    Calling Manager() without argument causes a compilation error due to missing parameter.
  3. Final Answer:

    Manager class missing argument for Worker in constructor call. -> Option A
  4. Quick Check:

    Constructor argument missing = compilation error [OK]
Hint: Check constructor parameters when using delegation [OK]
Common Mistakes:
  • Ignoring constructor parameters
  • Confusing delegation syntax with inheritance
  • Assuming manage() is inaccessible
5. You want to design a Kotlin class SmartDevice that can either behave like a Phone or a Camera depending on user choice at runtime. Which approach is best and why?
hard
A. Use static methods in SmartDevice to mimic Phone and Camera features.
B. Use inheritance with SmartDevice extending Phone or Camera based on a flag.
C. Use multiple inheritance to inherit both Phone and Camera directly.
D. Use delegation to delegate Phone or Camera behavior to SmartDevice dynamically.

Solution

  1. Step 1: Analyze design needs

    SmartDevice needs to switch behavior dynamically between Phone and Camera at runtime.
  2. Step 2: Choose delegation for flexibility

    Delegation allows SmartDevice to hold and switch between Phone or Camera instances easily without inheritance constraints.
  3. Step 3: Exclude other options

    Inheritance is static and cannot switch base class at runtime; Kotlin does not support multiple inheritance; static methods do not provide instance behavior.
  4. Final Answer:

    Use delegation to delegate Phone or Camera behavior to SmartDevice dynamically. -> Option D
  5. Quick Check:

    Dynamic behavior switching = delegation [OK]
Hint: Use delegation for flexible runtime behavior changes [OK]
Common Mistakes:
  • Trying to use inheritance for runtime behavior switch
  • Assuming Kotlin supports multiple inheritance
  • Using static methods instead of instance delegation
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard