Kotlin - Understanding the "open" keyword

Paul Franco - Sep 16 '20 - - Dev Community

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.

. . . . . .
Terabox Video Player