Inheritance allows a class to inherit properties and functions from a parent class, enabling code reuse and extension.
Click to reveal answer
beginner
What is delegation in Kotlin?
Delegation means a class hands off some tasks to another class or object, using its functionality instead of inheriting it.
Click to reveal answer
intermediate
When should you prefer delegation over inheritance?
Use delegation when you want to reuse behavior without creating a tight parent-child relationship, or when you want more flexibility and less coupling.
Click to reveal answer
intermediate
What is a key downside of inheritance?
Inheritance can create tight coupling between classes, making code harder to change or extend later.
Click to reveal answer
intermediate
How does Kotlin support delegation natively?
Kotlin has a special syntax called the <code>by</code> keyword that lets a class delegate implementation of an interface to another object easily.
Click to reveal answer
Which keyword does Kotlin use for delegation?
Aimplements
Bextends
Cby
Ddelegate
✗ Incorrect
Kotlin uses the 'by' keyword to delegate interface implementation to another object.
What is a main advantage of delegation over inheritance?
AFaster execution speed
BMore memory usage
CSimpler syntax
DLess tight coupling between classes
✗ Incorrect
Delegation reduces tight coupling, making code more flexible and easier to maintain.
Inheritance creates what kind of relationship between classes?
AParent-child
BPeer-to-peer
CNo relationship
DDelegation
✗ Incorrect
Inheritance creates a parent-child relationship where the child class inherits from the parent.
When might inheritance be a better choice than delegation?
AWhen you want to share behavior without hierarchy
BWhen you want to extend or modify behavior in a clear hierarchy
CWhen you want to avoid tight coupling
DWhen you want to delegate tasks dynamically
✗ Incorrect
Inheritance is good when you want a clear class hierarchy and to extend or override behavior.
Which of these is NOT a benefit of delegation?
ATighter coupling between classes
BAvoids deep inheritance trees
CEasier to change behavior at runtime
DMore flexible code reuse
✗ Incorrect
Delegation reduces coupling, it does not increase it.
Explain the difference between delegation and inheritance in Kotlin and when to use each.
Think about how classes relate and share behavior.
You got /4 concepts.
Describe how Kotlin's 'by' keyword helps with delegation.
Focus on the language feature that makes delegation easy.
You got /4 concepts.
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
Step 1: Understand inheritance purpose
Inheritance models an "is-a" relationship where a subclass extends a superclass.
Step 2: Match the correct use case
Using inheritance is appropriate when a class is a specialized version of another class.
Final Answer:
When a class is a specialized type of another class ("is-a" relationship). -> Option C
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
Step 1: Recall Kotlin delegation syntax
Kotlin uses : Interface by instance to delegate interface implementation.
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.
Final Answer:
class Document(val printer: Printer) : Printer by printer -> Option B
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
Step 1: Understand delegation in Service class
Service delegates Logger interface to the passed logger instance, here ConsoleLogger.
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
Step 1: Analyze design needs
SmartDevice needs to switch behavior dynamically between Phone and Camera at runtime.
Step 2: Choose delegation for flexibility
Delegation allows SmartDevice to hold and switch between Phone or Camera instances easily without inheritance constraints.
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.
Final Answer:
Use delegation to delegate Phone or Camera behavior to SmartDevice dynamically. -> Option D
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 unknownstrongwhile earning +1 Grip toward your certificate.