0
0
Kotlinprogramming~20 mins

Why open keyword is required for inheritance in Kotlin - Challenge Your Understanding

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Kotlin Inheritance Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output of this Kotlin code involving open keyword?
Consider the following Kotlin code snippet. What will be the output when it runs?
Kotlin
open class Animal {
    open fun sound() = "Some sound"
}

class Dog : Animal() {
    override fun sound() = "Bark"
}

fun main() {
    val dog = Dog()
    println(dog.sound())
}
ACompilation error: Class 'Animal' is not open for inheritance
BSome sound
CBark
DRuntime error
Attempts:
2 left
💡 Hint
Remember that in Kotlin classes are final by default unless marked open.
Predict Output
intermediate
2:00remaining
What error occurs if open keyword is missing on a Kotlin class?
What happens if you try to inherit from a Kotlin class that is not marked with the open keyword?
Kotlin
class Vehicle {
    fun drive() = "Driving"
}

class Car : Vehicle() {}

fun main() {
    val car = Car()
    println(car.drive())
}
AOutput: Driving
BCompilation error: Function drive() must be open
CRuntime error: Inheritance not allowed
DCompilation error: Class 'Vehicle' is not open for inheritance
Attempts:
2 left
💡 Hint
Kotlin classes are final by default.
🔧 Debug
advanced
2:30remaining
Identify the cause of the compilation error in this Kotlin inheritance code
Why does this Kotlin code fail to compile?
Kotlin
class Parent {
    fun greet() = "Hello"
}

class Child : Parent() {
    override fun greet() = "Hi"
}

fun main() {
    val child = Child()
    println(child.greet())
}
ACannot override function greet() because it is not marked open in Parent
BCannot override final function greet() because Parent is not open
CCannot inherit from Parent because it is not open
DNo error, code runs and prints "Hi"
Attempts:
2 left
💡 Hint
Kotlin classes are final by default.
🧠 Conceptual
advanced
2:00remaining
Why does Kotlin require the open keyword for inheritance?
Which of the following best explains why Kotlin requires the open keyword on classes and functions to allow inheritance and overriding?
ATo make all classes and functions final by default for safer code
BTo improve runtime performance by preventing unintended inheritance
CTo allow multiple inheritance of classes
DTo enable automatic memory management
Attempts:
2 left
💡 Hint
Think about Kotlin's design choice for safety.
Predict Output
expert
3:00remaining
What is the output of this Kotlin code with open and override keywords?
Analyze the following Kotlin code and determine its output.
Kotlin
open class A {
    open fun foo() = "A"
}

open class B : A() {
    override fun foo() = "B"
}

class C : B() {
    override fun foo() = "C"
}

fun main() {
    val obj: A = C()
    println(obj.foo())
}
AA
BC
CB
DCompilation error due to missing open keyword
Attempts:
2 left
💡 Hint
Remember that the actual object's method is called, not the reference type's.