0
0
Kotlinprogramming~10 mins

Why open keyword is required for inheritance in Kotlin - Visual Breakdown

Choose your learning style9 modes available
Concept Flow - Why open keyword is required for inheritance
Define class
Is class marked 'open'?
NoCannot inherit, error
Yes
Create subclass
Subclass inherits properties and methods
Use subclass
Kotlin requires the 'open' keyword on a class to allow inheritance. Without it, the class is final and cannot be subclassed.
Execution Sample
Kotlin
open class Animal {
    fun sound() = "Some sound"
}

class Dog : Animal() {
    fun bark() = "Woof"
}
Defines an open class Animal and a subclass Dog that inherits from Animal.
Execution Table
StepActionEvaluationResult
1Define class Animal without 'open'class Animal {}Class is final by default
2Try to inherit Animal in class Dog : Animal()Error: Cannot inherit from final class AnimalCompilation error
3Add 'open' keyword to Animal: open class Animalclass Animal is now openInheritance allowed
4Define subclass Dog : Animal()Dog inherits Animal's membersDog can use sound() method
5Create Dog instance and call bark()Dog().bark()Returns 'Woof'
6Create Dog instance and call sound()Dog().sound()Returns 'Some sound'
💡 Inheritance fails without 'open' keyword because classes are final by default in Kotlin.
Variable Tracker
VariableStartAfter Step 3After Step 4After Step 5Final
Animal classfinalopenopenopenopen
Dog classundefinedundefineddefined subclassdefined subclassdefined subclass
Dog instancenonenonenonecreatedcreated
Key Moments - 2 Insights
Why does Kotlin require the 'open' keyword to inherit a class?
By default, Kotlin classes are final to prevent unintended inheritance. The 'open' keyword explicitly allows inheritance, as shown in execution_table step 3 where adding 'open' enables subclassing.
What happens if you try to inherit a class without 'open'?
You get a compilation error because the class is final. This is shown in execution_table step 2 where inheritance fails without 'open'.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, at which step does Kotlin allow inheritance?
AStep 2
BStep 3
CStep 1
DStep 5
💡 Hint
Check the 'Evaluation' column where 'class Animal is now open' appears.
According to variable_tracker, what is the state of the Animal class before step 3?
Afinal
Babstract
Copen
Dsealed
💡 Hint
Look at the 'Animal class' row under 'Start' column.
If we remove the 'open' keyword from Animal, what will happen when defining Dog?
ADog inherits Animal successfully
BDog becomes abstract
CDog cannot inherit Animal, compilation error
DDog overrides Animal automatically
💡 Hint
Refer to execution_table step 2 where inheritance fails without 'open'.
Concept Snapshot
Kotlin classes are final by default.
Use 'open' keyword to allow inheritance.
Without 'open', subclassing causes error.
Example: open class Animal { } allows class Dog : Animal() { }.
This design prevents accidental inheritance.
Full Transcript
In Kotlin, classes cannot be inherited unless marked with the 'open' keyword. By default, classes are final, meaning you cannot create subclasses from them. This prevents accidental or unwanted inheritance. When you want a class to be inheritable, you add 'open' before the class keyword. For example, 'open class Animal' allows another class like 'Dog' to inherit from it. If you try to inherit from a class without 'open', the compiler will give an error. This behavior is shown step-by-step in the execution table and variable tracker. Remember, 'open' explicitly signals that inheritance is allowed.