Inheritance
When you use inheritance, you’ll feel the extra layer of safety and protection that Kotlin provides. Since inheritance is one of the misused concepts in OO programming, Kotlin helps you make sure that your intentions are laid out very explicitly to the users of your classes.
Kotlin doesn’t want classes to accidentally serve as a base class. As an author of a class, you have to provide explicit permission for your class to be used as a base class. Likewise, when writing a method, you have to tell Kotlin that it’s OK for a derived class to override that method. Let’s take a look at how Kotlin provides this safety net.
Unlike interfaces, classes in Kotlin are final by default—that is, you can’t inherit from them. Only classes marked ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access