Bird
Raised Fist0
Kotlinprogramming~20 mins

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

Choose your learning style10 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.

Practice

(1/5)
1. Why is the open keyword required in Kotlin for inheritance?
easy
A. Because classes are final by default and cannot be inherited without open
B. Because open makes the class private
C. Because open automatically creates a constructor
D. Because open changes the class to an interface

Solution

  1. Step 1: Understand Kotlin class default behavior

    In Kotlin, classes are final by default, meaning they cannot be inherited unless explicitly allowed.
  2. Step 2: Role of the open keyword

    The open keyword marks a class as inheritable, allowing other classes to extend it.
  3. Final Answer:

    Because classes are final by default and cannot be inherited without open -> Option A
  4. Quick Check:

    Default final class = need open for inheritance [OK]
Hint: Remember: classes are final unless marked open [OK]
Common Mistakes:
  • Thinking open makes class private
  • Confusing open with interface keyword
  • Assuming inheritance works without open
2. Which of the following is the correct way to declare a class that can be inherited in Kotlin?
easy
A. open class MyClass {}
B. private class MyClass {}
C. final class MyClass {}
D. class MyClass {}

Solution

  1. Step 1: Check class declaration for inheritance

    To allow inheritance, the class must be marked with open.
  2. Step 2: Identify correct syntax

    Only open class MyClass {} allows other classes to inherit from it.
  3. Final Answer:

    open class MyClass {} -> Option A
  4. Quick Check:

    Use open keyword to allow inheritance [OK]
Hint: Look for open keyword to allow inheritance [OK]
Common Mistakes:
  • Omitting open keyword
  • Using final which forbids inheritance
  • Using private which restricts visibility
3. What will happen when you run this Kotlin code?
class Parent {}
class Child : Parent() {}
medium
A. Code runs successfully and Child inherits Parent
B. Runtime error due to missing constructor
C. Child class becomes abstract automatically
D. Compilation error because Parent is final by default

Solution

  1. Step 1: Analyze class inheritance without open

    Parent class is not marked open, so it is final by default and cannot be inherited.
  2. Step 2: Understand compilation behavior

    Trying to inherit from a final class causes a compilation error in Kotlin.
  3. Final Answer:

    Compilation error because Parent is final by default -> Option D
  4. Quick Check:

    Final class inheritance causes compile error [OK]
Hint: No open means no inheritance allowed [OK]
Common Mistakes:
  • Assuming inheritance works without open
  • Confusing runtime error with compile error
  • Thinking child class auto becomes abstract
4. Identify the error in this Kotlin code and fix it:
class Animal {}
class Dog : Animal() {}
medium
A. Remove parentheses from Dog inheritance
B. Add open keyword to Animal class
C. Make Dog class final
D. Change Animal to an interface

Solution

  1. Step 1: Identify inheritance error

    Animal class is final by default, so Dog cannot inherit from it without open.
  2. Step 2: Fix by adding open keyword

    Mark Animal as open class Animal {} to allow inheritance.
  3. Final Answer:

    Add open keyword to Animal class -> Option B
  4. Quick Check:

    Use open to fix inheritance error [OK]
Hint: Add open to parent class for inheritance [OK]
Common Mistakes:
  • Removing parentheses instead of adding open
  • Making child class final instead of parent open
  • Changing class to interface unnecessarily
5. You want to create a base class Vehicle that others can extend, but only allow extension within your module. How should you declare it in Kotlin?
hard
A. open class Vehicle {}
B. public open class Vehicle {}
C. internal open class Vehicle {}
D. final class Vehicle {}

Solution

  1. Step 1: Understand visibility and inheritance

    To allow inheritance only within the module, use internal visibility modifier.
  2. Step 2: Combine internal with open

    Mark class as internal open class Vehicle {} to allow inheritance inside the module but not outside.
  3. Final Answer:

    internal open class Vehicle {} -> Option C
  4. Quick Check:

    Use internal open for module-only inheritance [OK]
Hint: Use internal open for inheritance limited to module [OK]
Common Mistakes:
  • Using public open allows inheritance everywhere
  • Using final forbids inheritance
  • Omitting open keyword disables inheritance
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard