DEV Community

Paul Franco
Paul Franco

Posted on

Kotlin - Understanding the "open" keyword

Inheritance is the cornerstone of Object-Oriented Programming . OOP is the process of acquiring or using the properties and characteristics of one class by another.

In Kotlin, the classes, functions, and variables are final by default meaning that they cannot be inherited from by any other class. In order to make a class inheritable to the other classes, you must mark it with the open keyword otherwise you will get an error saying “type is final so can’t be inherited”.

Alt Text

As seen in the image above. RaceCar inherits from Vehicle but no other class can inherit from RaceCar because the RaceCar class is not marked with the open keyword. In the RaceCar class we are able to override the driveFast() function because it is marked with the open keyword inside the parent class Vehicle.

Discussion (2)

ender_minyard profile image
ender minyard • Edited

Great article!

paulfranco profile image
Paul Franco Author

I'll give that a try. Thanks.