📖 Scenario: Imagine you are creating a simple app with animals. You want to make a base class Animal and then create a specific animal like Dog that inherits from Animal.In Kotlin, classes are final by default, so you need to use the open keyword to allow inheritance.
🎯 Goal: Learn how to use the open keyword in Kotlin to allow a class to be inherited.
📋 What You'll Learn
Create an open class called Animal with a function makeSound() that prints "Some sound"
Create a class called Dog that inherits from Animal
Override the makeSound() function in Dog to print "Bark"
Create an instance of Dog and call makeSound() to see the output
💡 Why This Matters
🌍 Real World
Understanding the <code>open</code> keyword is important when designing apps that use inheritance, like games with different characters or apps with reusable components.
💼 Career
Many Kotlin jobs require knowledge of class inheritance and how to control it with <code>open</code> and <code>final</code> keywords to write safe and maintainable code.
Progress0 / 4 steps
1
Create an open class Animal
Create an open class called Animal with a function makeSound() that prints "Some sound".
Kotlin
Hint
Remember to use open before the class and the function to allow inheritance and overriding.
2
Create a class Dog that inherits from Animal
Create a class called Dog that inherits from Animal.
Kotlin
Hint
Use class Dog : Animal() to inherit from Animal.
3
Override makeSound() in Dog
Override the makeSound() function in Dog to print "Bark".
Kotlin
Hint
Use override fun makeSound() inside Dog to change the behavior.
4
Create Dog instance and call makeSound()
Create an instance of Dog called dog and call dog.makeSound() to print the sound.
Kotlin
Hint
Create val dog = Dog() and then call dog.makeSound() inside main().
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
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 open keyword
The open keyword marks a class as inheritable, allowing other classes to extend it.
Final Answer:
Because classes are final by default and cannot be inherited without open -> Option A
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
Step 1: Check class declaration for inheritance
To allow inheritance, the class must be marked with open.
Step 2: Identify correct syntax
Only open class MyClass {} allows other classes to inherit from it.
Final Answer:
open class MyClass {} -> Option A
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
Step 1: Analyze class inheritance without open
Parent class is not marked open, 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 D
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
Step 1: Identify inheritance error
Animal class is final by default, so Dog cannot inherit from it without open.
Step 2: Fix by adding open keyword
Mark Animal as open class Animal {} to allow inheritance.
Final Answer:
Add open keyword to Animal class -> Option B
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
Step 1: Understand visibility and inheritance
To allow inheritance only within the module, use internal visibility modifier.
Step 2: Combine internal with open
Mark class as internal open class Vehicle {} to allow inheritance inside the module but not outside.
Final Answer:
internal open class Vehicle {} -> Option C
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 unknownstrongwhile earning +1 Grip toward your certificate.