What is inheritance in Kotlin?

Kotlin is an open-source, statically-typed programming language that supports both object-oriented and functional programming.

Kotlin does not aim to be unique, instead it draws inspiration from decades of language development.

To know more about Object-oriented Programming, check out my shot on Object-Oriented Programming where I talk about various Object-Oriented Concepts and use cases.

At the end of this article, you will know how Inheritance works using Kotlin as a programming language. Inheritance is a mechanism where you derive a class from another class for a hierarchy of classes that share a set of attributes and methods.

Using an Aquarium class

open class Aquarium(var length: Int = 100, var width: Int = 20, var height: Int = 40){
open var volume: Int
get() = width * height * length / 1000
set(value) {height = (value * 1000) / (width * length)}
open var water = volume * 0.9
constructor (numberOfFish: Int) : this(){
val water = numberOfFish * 2000
val tank = water + water * 0.1
height = (tank / (length * width)).toInt()
}
}
fun main(args : Array<String>) {
val aq = Aquarium()
println(aq.water)
}

The open keyword is to explicitly allow this class to be inherited along with its properties.

Let’s say we want to have a different type of Aquarium, such as a Cylindrical Tower. Tower Tanks are a lot like regular Aquariums, but they are also different in some ways.

We can inherit a lot of stuff from our basic Aquarium and change the things that are different.

Creating a child class, TowerTank

open class Aquarium(var length: Int = 100, var width: Int = 20, var height: Int = 40){
open var volume: Int
get() = width * height * length / 1000
set(value) {height = (value * 1000) / (width * length)}
open var water = volume * 0.9
constructor (numberOfFish: Int) : this(){
val water = numberOfFish * 2000
val tank = water + water * 0.1
height = (tank / (length * width)).toInt()
}
}
class TowerTank() : Aquarium(){
override var volume: Int
get() = (width * height * length / 1000 * 3.142).toInt()
set(value) {height = (value * 1000) / (width * length)}
override var water = volume * 0.8
}
fun main(args : Array<String>) {
val tank = TowerTank()
println(tank.water)
}

Parent class (also called base class) is the class being inherited from. The child class (also called derived class) inherits from another class.

  • class TowerTank() : Aquarium(): Here, the TowerTank class inherits the Aquarium class.

  • override - Enables a child class to give its own implementation to the already existing code of the base class.

  • TowerTank inherits properties like water and volume from the Aquarium class, but with its own implementation.

The main advantages of inheritance are code reusability and readability. When a child class inherits the properties and functionality of the parent class, we don’t need to write the same code again in the child class. This makes it easier to reuse the code, makes us write less code, and makes the code much more readable.

Knowledge Check:

1

Which keyword enables a class to be inherited?

A)

open

B)

get

C)

import

Question 1 of 20 attempted

You have successfully learned how to implement inheritance using Kotlin!

Free Resources