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.
Aquarium
classopen class Aquarium(var length: Int = 100, var width: Int = 20, var height: Int = 40){open var volume: Intget() = width * height * length / 1000set(value) {height = (value * 1000) / (width * length)}open var water = volume * 0.9constructor (numberOfFish: Int) : this(){val water = numberOfFish * 2000val tank = water + water * 0.1height = (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.
open class Aquarium(var length: Int = 100, var width: Int = 20, var height: Int = 40){open var volume: Intget() = width * height * length / 1000set(value) {height = (value * 1000) / (width * length)}open var water = volume * 0.9constructor (numberOfFish: Int) : this(){val water = numberOfFish * 2000val tank = water + water * 0.1height = (tank / (length * width)).toInt()}}class TowerTank() : Aquarium(){override var volume: Intget() = (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:
Which keyword enables a class to be inherited?
open
get
import
You have successfully learned how to implement inheritance using Kotlin!