Back to questions
JuniorTheoryCommon
What does the program print?
interface Greeter {
fun name(): String
fun greet() = "Hi, ${name()}"
}
class Bot : Greeter {
override fun name() = "Bot"
}
fun main() {
val g: Greeter = Bot()
println(g.greet())
}Hi, Bot
- Expecting a compile error: thinking a class must override every method of its interface. Only methods without a body must be overridden;
greet()comes ready-made. - Expecting
Hi, name(): thinking a call inside a string is printed as text. In a string template,${name()}runs the call and inserts what it returns.
- What does the program print if
Botalso overridesgreet()to return"Beep"? - Can one class implement two interfaces at once?