avatarGabriel Shanahan

Free AI web copilot to create summaries, insights and extended knowledge, download it at here

2069

Abstract

url=https%3A%2F%2Fpl.kotl.in%2FVG_7yTWo5&image=https%3A%2F%2Fplay.kotlinlang.org%2Fassets%2Fog-image.png&key=a19fcc184b9711e1b4764040d3dc5c07&type=text%2Fhtml&schema=kotl" allowfullscreen="" frameborder="0" height="300" width="800"> </div> </div> </figure></iframe></div></div></figure><p id="af9e">A method marked <code>override</code> is automatically <code>open</code>. To prohibit that, use <code>final override</code> instead.</p><h1 id="585c">Properties</h1><p id="0b6b">As with classes and methods, properties which can be overridden must be marked as <code>open</code> and overriding properties with <code>override</code>. Keep in mind that properties are actually (one or two) functions, so there are various ways you can override them.</p> <figure id="b5b7"> <div> <div> <img class="ratio" src="http://placehold.it/16x9"> <iframe class="" src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fpl.kotl.in%2FzXKwv9h-I&amp;display_name=Kotlin+Playground&amp;url=https%3A%2F%2Fpl.kotl.in%2FzXKwv9h-I&amp;image=https%3A%2F%2Fplay.kotlinlang.org%2Fassets%2Fog-image.png&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=kotl" allowfullscreen="" frameborder="0" height="300" width="800"> </div> </div> </figure></iframe></div></div></figure><p id="819a">You can also override a <code>val</code> property with a <code>var</code> property, but not vice versa. This is allowed because a <code>val</code> property essentially declares a get method, and overriding it as a <code>var</code> additionally declares a set method in the derived class.</p><h1 id="61b6">Calling the superclass implementation</h1><p id="8454">…can be done using <code>super</code>:</p> <figure id="98ab"> <div> <div> <img class="ratio" src="http://placehold.it/16x9"> <iframe class="" src="https://cdn.embedly.com/widgets/media.html?src=https%3

Options

A%2F%2Fpl.kotl.in%2FI9F4uITgD&display_name=Kotlin+Playground&url=https%3A%2F%2Fpl.kotl.in%2FI9F4uITgD&image=https%3A%2F%2Fplay.kotlinlang.org%2Fassets%2Fog-image.png&key=a19fcc184b9711e1b4764040d3dc5c07&type=text%2Fhtml&schema=kotl" allowfullscreen="" frameborder="0" height="300" width="800"> </div> </div> </figure></iframe></div></div></figure><h1 id="0b58">Multiple inheritance</h1><p id="8789">If a class inherits multiple implementations of the same member from its immediate parents, it must override this member and provide its own implementation (perhaps, using one of the inherited ones). The parent implementations are qualified using <code>super<ParentClass>.parentMethod()</code></p> <figure id="db8b"> <div> <div> <img class="ratio" src="http://placehold.it/16x9"> <iframe class="" src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fpl.kotl.in%2FYy-fzqLyv&amp;display_name=Kotlin+Playground&amp;url=https%3A%2F%2Fpl.kotl.in%2FYy-fzqLyv&amp;image=https%3A%2F%2Fplay.kotlinlang.org%2Fassets%2Fog-image.png&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=kotl" allowfullscreen="" frameborder="0" height="300" width="800"> </div> </div> </figure></iframe></div></div></figure><p id="1eb9">Go back to <a href="https://readmedium.com/inheritance-basics-8d5dc28d326e">Inheritance — basics</a>, jump to the <a href="https://readmedium.com/table-of-contents-c52573cfa291">Table of Contents</a>, or continue to <a href="https://readmedium.com/interfaces-936fe79a6450">Interfaces</a>.</p><figure id="8ecd"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*biBSB579iezsNvEQ_NMLBg.png"><figcaption><a href="https://www.etnetera.cz/prace-u-nas?utm_source=medium&amp;utm_medium=GabrielShanahan&amp;utm_campaign=KotlinPrimer&amp;utm_content=join-our-team&amp;utm_term=KotlinPrimer#pozice">Join me in Etnetera</a></figcaption></figure></article></body>

Overriding

Overriding methods, properties, calling the superclass implementation, and how to do so when using multiple inheritance

— — — — — — — — — — — — — — —

THE CURRENT VERSION OF THIS ARTICLE IS PUBLISHED HERE.

— — — — — — — — — — — — — — —

Tags: #FYI

This article is part of the Kotlin Primer, an opinionated guide to the Kotlin language, which is indented to help facilitate Kotlin adoption inside Java-centric organizations. It was originally written as an organizational learning resource for Etnetera a.s. and I would like to express my sincere gratitude for their support.

It is recommended to read the Introduction before moving on. Check out the Table of Contents for all articles.

Methods

Methods are also closed (final) by default, and must be marked as open to allow overriding. In the same manner, methods which override a base method must be marked by override.

A method marked override is automatically open. To prohibit that, use final override instead.

Properties

As with classes and methods, properties which can be overridden must be marked as open and overriding properties with override. Keep in mind that properties are actually (one or two) functions, so there are various ways you can override them.

You can also override a val property with a var property, but not vice versa. This is allowed because a val property essentially declares a get method, and overriding it as a var additionally declares a set method in the derived class.

Calling the superclass implementation

…can be done using super:

Multiple inheritance

If a class inherits multiple implementations of the same member from its immediate parents, it must override this member and provide its own implementation (perhaps, using one of the inherited ones). The parent implementations are qualified using super<ParentClass>.parentMethod()

Go back to Inheritance — basics, jump to the Table of Contents, or continue to Interfaces.

Join me in Etnetera
Kotlin
Java
Programming
Overriding
Object Oriented
Recommended from ReadMedium