Kotlin - Understanding the "open" keyword
Paul Franco

Paul Franco @paulfranco

About: I am an Android application developer based out of Seattle. I specialize in building Full Stack Mobile applications with NodeJS and Firebase back-ends.

Location:
Seattle, WA
Joined:
Sep 1, 2020

Kotlin - Understanding the "open" keyword

Publish Date: Sep 16 '20
2 2

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.

Comments 2 total

Add comment