Back to questions
JuniorTheoryCommon
What happens when you build and run this program?
class Animal {
fun sound() = "..."
}
class Dog : Animal()
fun main() {
println(Dog().sound())
}It does not compile: Animal is not open
- Expecting
...: thinking any class can be extended. In Kotlin a class is closed until it is markedopen, so: Animal()stops the build. - Expecting the program to start and then fail: the compiler checks the class header first, so nothing is built and nothing runs.
- Which single word makes this program compile, and where does it go?
- What else must you add before
Dogcan replacesound()with its own version?