Search icon CANCEL
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Kotlin Quick Start Guide

You're reading from   Kotlin Quick Start Guide Core features to get you ready for developing applications

Arrow left icon
Product type Paperback
Published in Aug 2018
Publisher Packt
ISBN-13 9781789344189
Length 178 pages
Edition 1st Edition
Languages
Tools
Arrow right icon
Author (1):
Arrow left icon
Marko Devcic Marko Devcic
Author Profile Icon Marko Devcic
Marko Devcic
Arrow right icon
View More author details
Toc

Overriding members


Reimplementing functions or properties from a base type is called overriding. Overriding is also a way to achieve polymorphism, another aspect of object-oriented programming. In Kotlin, you can override them only if the base type allows it. By default, all members in Kotlin are final, that is, cannot be overridden. To allow a subclass to override a member, it has to have the open keyword. This is in contrast to Java, where by default every member is overridable. Another difference from Java is that whenever you are overriding a member, you have to have the override keyword present. Java doesn't have such a keyword and overriding is considered with just declaring a function with the same name and signatures as the base type. This can be error-prone and that's why Java uses the @Override annotation to make the developer's intentions clearer:

open class Base {
open fun print() {
println("I'm called from the base type")
    }
}

class Derived : Base() {
override fun print(...
lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at €18.99/month. Cancel anytime