Predict the output
What does this print?
open class Shape
class Circle : Shape()
fun Shape.name() = "shape"
fun Circle.name() = "circle"
fun main() {
val s: Shape = Circle()
println(s.name())
}Hint 1 of 1
Is the call decided at compile time or at runtime?
Choose an answer, then open Show answer below to check it.
Show answer
Answer: A shape
Correct. Extensions are resolved by the declared type, which is Shape.
Why not the others?
- B circle. That would be a member override; extensions aren't virtual.
- C It doesn't compile: ambiguous call. Each extension has a different receiver type, so there's no ambiguity.
- D Circle@1a2b3c. name() returns a String literal.