Back to questions
JuniorTheoryCommon
What does the program print?
open class Shape {
open fun name() = "shape"
fun describe() = "I'm a ${name()}"
}
class Circle : Shape() {
override fun name() = "circle"
}
fun main() {
val s: Shape = Circle()
println(s.describe())
}I'm a circle
- Expecting
I'm a shape: thinking the typeShapeofs, ordescribe()living inShape, picksname(). The object is aCircle, so its override runs. - Expecting a compile error: thinking a
Shapevariable cannot hold aCircle. ACircleis aShape, so the assignment is allowed.
- What happens if
openis removed fromname()inShape? - What does
Shape().describe()print?