What if your code could protect itself from accidental changes while still letting you build on it easily?
Why open keyword is required for inheritance in Kotlin - The Real Reasons
Imagine you have a class in Kotlin that you want to reuse and add new features to by creating a child class. But when you try to inherit from it, the compiler stops you. This feels like trying to build a new room on a house that was never designed to be expanded.
Without the open keyword, Kotlin classes are final by default. This means you cannot inherit from them. Trying to do so causes errors, making it frustrating to extend functionality or reuse code. It forces you to rewrite or duplicate code instead of building on what exists.
The open keyword in Kotlin explicitly marks a class as inheritable. It tells the compiler, "This class is designed to be extended." This clear signal prevents accidental inheritance and makes your code safer and easier to maintain.
class Animal { fun sound() = println("Some sound") } class Dog : Animal() {} // Error: Cannot inherit from final class
open class Animal { fun sound() = println("Some sound") } class Dog : Animal() { fun bark() = println("Woof") }
Using open lets you safely create new classes that build on existing ones, enabling flexible and reusable code design.
Think of a video game where you have a basic Character class. Marking it open allows you to create specialized characters like Wizard or Warrior that inherit common traits but add unique abilities.
Kotlin classes are final by default to prevent unintended inheritance.
The open keyword explicitly allows a class to be inherited.
This approach improves code safety and clarity while enabling reuse.
Practice
open keyword required in Kotlin for inheritance?Solution
Step 1: Understand Kotlin class default behavior
In Kotlin, classes are final by default, meaning they cannot be inherited unless explicitly allowed.Step 2: Role of the
Theopenkeywordopenkeyword marks a class as inheritable, allowing other classes to extend it.Final Answer:
Because classes are final by default and cannot be inherited withoutopen-> Option AQuick Check:
Default final class = needopenfor inheritance [OK]
- Thinking
openmakes class private - Confusing
openwith interface keyword - Assuming inheritance works without
open
Solution
Step 1: Check class declaration for inheritance
To allow inheritance, the class must be marked withopen.Step 2: Identify correct syntax
Onlyopen class MyClass {}allows other classes to inherit from it.Final Answer:
open class MyClass {} -> Option AQuick Check:
Useopenkeyword to allow inheritance [OK]
open keyword to allow inheritance [OK]- Omitting
openkeyword - Using
finalwhich forbids inheritance - Using
privatewhich restricts visibility
class Parent {}
class Child : Parent() {}Solution
Step 1: Analyze class inheritance without
Parent class is not markedopenopen, so it is final by default and cannot be inherited.Step 2: Understand compilation behavior
Trying to inherit from a final class causes a compilation error in Kotlin.Final Answer:
Compilation error because Parent is final by default -> Option DQuick Check:
Final class inheritance causes compile error [OK]
open means no inheritance allowed [OK]- Assuming inheritance works without
open - Confusing runtime error with compile error
- Thinking child class auto becomes abstract
class Animal {}
class Dog : Animal() {}Solution
Step 1: Identify inheritance error
Animal class is final by default, so Dog cannot inherit from it withoutopen.Step 2: Fix by adding
Mark Animal asopenkeywordopen class Animal {}to allow inheritance.Final Answer:
Addopenkeyword to Animal class -> Option BQuick Check:
Useopento fix inheritance error [OK]
open to parent class for inheritance [OK]- Removing parentheses instead of adding
open - Making child class final instead of parent open
- Changing class to interface unnecessarily
Vehicle that others can extend, but only allow extension within your module. How should you declare it in Kotlin?Solution
Step 1: Understand visibility and inheritance
To allow inheritance only within the module, useinternalvisibility modifier.Step 2: Combine
Mark class asinternalwithopeninternal open class Vehicle {}to allow inheritance inside the module but not outside.Final Answer:
internal open class Vehicle {} -> Option CQuick Check:
Useinternal openfor module-only inheritance [OK]
internal open for inheritance limited to module [OK]- Using public open allows inheritance everywhere
- Using final forbids inheritance
- Omitting open keyword disables inheritance
Ready to prove it?
Pass the test and this pattern moves unknown
