Bird
Raised Fist0
Kotlinprogramming~20 mins

Open classes and methods in Kotlin - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Open Classes Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of overriding an open method
What is the output of this Kotlin code?
Kotlin
open class Animal {
    open fun sound() = "Some sound"
}

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

fun main() {
    val animal: Animal = Dog()
    println(animal.sound())
}
ARuntime error
BBark
CCompilation error
DSome sound
Attempts:
2 left
💡 Hint
Remember that open methods can be overridden in subclasses.
❓ Predict Output
intermediate
2:00remaining
Effect of missing open keyword on inheritance
What happens when you try to compile and run this Kotlin code?
Kotlin
class Vehicle {
    fun drive() = "Driving"
}

class Car : Vehicle() {
    override fun drive() = "Car driving"
}

fun main() {
    val car = Car()
    println(car.drive())
}
ACompilation error: 'drive' in 'Vehicle' is final and cannot be overridden
BDriving
CCar driving
DRuntime error
Attempts:
2 left
💡 Hint
Check if the method drive() is open for overriding.
🔧 Debug
advanced
2:00remaining
Why does this override fail?
This Kotlin code fails to compile. What is the reason?
Kotlin
open class Parent {
    fun greet() = "Hello"
}

class Child : Parent() {
    override fun greet() = "Hi"
}
AParent class must be abstract to allow overrides
BChild class must be open to override methods
CMissing override keyword causes error
Dgreet() in Parent is not open, so it cannot be overridden
Attempts:
2 left
💡 Hint
Check the declaration of greet() in Parent.
🧠 Conceptual
advanced
1:30remaining
Open classes and inheritance behavior
Which statement about open classes and methods in Kotlin is true?
AClasses and methods are open by default, so they can be inherited and overridden without keywords
BOnly classes are final by default; methods are open by default
CClasses and methods are final by default; you must mark them open to allow inheritance or overriding
DYou cannot inherit from open classes
Attempts:
2 left
💡 Hint
Think about Kotlin's default behavior for classes and methods.
❓ Predict Output
expert
2:30remaining
Output with open class and method with property override
What is the output of this Kotlin program?
Kotlin
open class Person {
    open val greeting: String = "Hello"
    open fun greet() = greeting
}

class Student : Person() {
    override val greeting: String = "Hi"
    override fun greet() = "${greeting}, student!"
}

fun main() {
    val p: Person = Student()
    println(p.greet())
}
AHi, student!
BCompilation error
CHello
DHello, student!
Attempts:
2 left
💡 Hint
Remember that properties can be overridden and used in methods.

Practice

(1/5)
1. In Kotlin, what does the open keyword do when applied to a class or method?
easy
A. Prevents the class or method from being used
B. Makes the class or method private
C. Allows the class or method to be inherited or overridden
D. Automatically overrides the method in subclasses

Solution

  1. Step 1: Understand Kotlin class and method defaults

    By default, Kotlin classes and methods are final, meaning they cannot be inherited or overridden.
  2. Step 2: Role of the open keyword

    The open keyword explicitly allows a class to be inherited or a method to be overridden in subclasses.
  3. Final Answer:

    Allows the class or method to be inherited or overridden -> Option C
  4. Quick Check:

    open means inheritance allowed [OK]
Hint: Remember: open means 'can be changed' [OK]
Common Mistakes:
  • Thinking classes are open by default
  • Confusing open with override
  • Assuming open makes methods private
2. Which of the following is the correct way to declare an open method named display in Kotlin?
easy
A. open fun display() { }
B. fun display() open { }
C. fun open display() { }
D. open display fun() { }

Solution

  1. Step 1: Recall Kotlin method declaration syntax

    In Kotlin, the keyword fun starts the function declaration, followed by the function name and parentheses.
  2. Step 2: Position of open keyword

    The open keyword must come before fun to mark the method as open for overriding.
  3. Final Answer:

    open fun display() { } -> Option A
  4. Quick Check:

    open fun is correct order [OK]
Hint: Put open before fun for methods [OK]
Common Mistakes:
  • Placing open after fun
  • Putting open after method name
  • Using open without fun keyword
3. What will be the output of the following Kotlin code?
open class Animal {
    open fun sound() = "Some sound"
}

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

fun main() {
    val pet: Animal = Dog()
    println(pet.sound())
}
medium
A. Some sound
B. Bark
C. Compilation error
D. Runtime error

Solution

  1. Step 1: Understand class inheritance and method overriding

    The class Dog inherits from Animal and overrides the sound() method to return "Bark".
  2. Step 2: Polymorphism in action

    Even though pet is typed as Animal, it holds a Dog instance, so the overridden sound() method in Dog is called.
  3. Final Answer:

    Bark -> Option B
  4. Quick Check:

    Overridden method runs on subclass instance [OK]
Hint: Overridden methods run on actual object type [OK]
Common Mistakes:
  • Expecting base class method output
  • Confusing compile-time and run-time types
  • Forgetting to mark methods open
4. Identify the error in the following Kotlin code:
class Vehicle {
    open fun start() = "Starting"
}

class Car : Vehicle() {
    fun start() = "Car starting"
}
medium
A. Method start() in Car must be marked override
B. No error, code is correct
C. Car class cannot inherit from Vehicle
D. Vehicle class must be open to allow inheritance

Solution

  1. Step 1: Check if Vehicle class allows inheritance

    By default, Kotlin classes are final. Since Vehicle is not marked open, it cannot be inherited.
  2. Step 2: Analyze the inheritance attempt

    Car tries to inherit from Vehicle, which causes a compilation error because Vehicle is not open.
  3. Final Answer:

    Vehicle class must be open to allow inheritance -> Option D
  4. Quick Check:

    Classes must be open to inherit [OK]
Hint: Mark classes open to inherit them [OK]
Common Mistakes:
  • Forgetting to mark base class open
  • Assuming methods override without keyword
  • Ignoring Kotlin's default final classes
5. You want to create a Kotlin class Shape with an open method area() that returns 0.0 by default. Then create a subclass Circle that overrides area() to return the circle's area using its radius. Which code correctly implements this?
hard
A. open class Shape { open fun area() = 0.0 } class Circle(val radius: Double) : Shape() { override fun area() = 3.14 * radius * radius }
B. class Shape { fun area() = 0.0 } class Circle(val radius: Double) : Shape() { fun area() = 3.14 * radius * radius }
C. open class Shape { fun area() = 0.0 } class Circle(val radius: Double) : Shape() { override fun area() = 3.14 * radius * radius }
D. class Shape { open fun area() = 0.0 } class Circle(val radius: Double) : Shape() { override fun area() = 3.14 * radius * radius }

Solution

  1. Step 1: Mark base class and method open

    To allow inheritance and overriding, Shape must be open and its area() method must also be open.
  2. Step 2: Override method correctly in subclass

    Circle inherits from Shape and overrides area() using override keyword, calculating area using radius.
  3. Final Answer:

    Option A code correctly implements open class and method with override -> Option A
  4. Quick Check:

    Open base and override subclass method [OK]
Hint: Open base class and method, override in subclass [OK]
Common Mistakes:
  • Not marking class or method open
  • Missing override keyword in subclass
  • Trying to override non-open method
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