Left Index

Kotlin Inheritance

Inheritance is one of the important features in object-oriented programming. Inheritance enables code re-usability, it allows to inherit the features from an existing class(base class) to a new class(derived class). Derived class can inherit all the features of base class and can add some features of their own. The class from which the features are inherited is known as base class or super class or parent class and the class that inherits the features is known as derived class or sub class or child class.

Let's have and example and try to understand Inheritance. We create Animal as a base class and Dog, Cat and Tiger as child class. Now for all Dog, Cat and Tiger has common properties(data members) like color, maxAge and common behaviour(member function) like eat, drink etc...


In below diagram Dog, Cat and Tiger shown some common properties and behaviour.

So all common properties and function we will define into one class i.e. Animal class. and this class inherited into Dog, Cat and Tiger as shown in below diagram.

Kotlin Inheritance Example:
package com.theitbulls.kotlinex

fun main(args: Array<String>) {
	var dog = Dog("Black", 10)
	println("Dog color: ${dog.color}, max age of dog: ${dog.maxAge}")
	dog.sound()

	var cat = Cat("White", 8)
	println("Cat color: ${cat.color}, max age of cat: ${cat.maxAge}")
	cat.sound()

	var tiger = Tiger("Yellow", 20)
	println("Tiger color: ${tiger.color}, max age of tiger: ${tiger.maxAge}")
	tiger.sound()
}

open class Animal {
	var color: String
	var maxAge: Int

	constructor(color: String, maxAge: Int) {
		this.color = color
		this.maxAge = maxAge
	}
}

class Dog : Animal {
	constructor(color: String, maxAge: Int) : super(color, maxAge) {

	}
}

class Cat : Animal {
	constructor(color: String, maxAge: Int) : super(color, maxAge) {

	}
}

class Tiger : Animal {
	constructor(color: String, maxAge: Int) : super(color, maxAge) {

	}
}
Output:
Dog color: Black, max age of dog: 10
Cat color: White, max age of cat: 8
Tiger color: Yellow, max age of tiger: 20

Overriding Member Functions

If the base class and the derived class contains a member function (or property) with the same name, you can need to override the member function of the derived class using override keyword, and use open keyword for the member function of the base class.

Example:
package com.theitbulls.kotlinex

fun main(args: Array<String>) {
	var dog = Dog("Black", 10)
	println("Dog color: ${dog.color}, max age of dog: ${dog.maxAge}")
	dog.sound()

	var cat = Cat("White", 8)
	println("Cat color: ${cat.color}, max age of cat: ${cat.maxAge}")
	cat.sound()

	var tiger = Tiger("Yellow", 20)
	println("Tiger color: ${tiger.color}, max age of tiger: ${tiger.maxAge}")
	tiger.sound()
}

open class Animal {
	var color: String
	var maxAge: Int

	constructor(color: String, maxAge: Int) {
		this.color = color
		this.maxAge = maxAge
	}

	open fun sound() {
		println("Animal sounds")
	}
}

class Dog : Animal {
	constructor(color: String, maxAge: Int) : super(color, maxAge) {

	}

	override fun sound() {
		println("Dogs barks")
	}
}

class Cat : Animal {
	constructor(color: String, maxAge: Int) : super(color, maxAge) {

	}

	override fun sound() {
		println("Cat meow")
	}
}

class Tiger : Animal {
	constructor(color: String, maxAge: Int) : super(color, maxAge) {

	}

	override fun sound() {
		println("Tiger roar")
	}
}
Output:
Dog color: Black, max age of dog: 10
Dogs barks
Cat color: White, max age of cat: 8
Cat meow
Tiger color: Yellow, max age of tiger: 20
Tiger roar

Calling Members of Parent class from Child class

You can call functions (and access properties) of the base class from a child class using super keyword.

Example:
package com.theitbulls.kotlinex

fun main(args: Array<String>) {
	var dog = Dog("Black", 10)
	println("Dog color: ${dog.color}, max age of dog: ${dog.maxAge}")
	dog.sound()
}

open class Animal {
	var color: String
	var maxAge: Int

	constructor(color: String, maxAge: Int) {
		this.color = color
		this.maxAge = maxAge
	}

	open fun sound() {
		print("Animal sounds: ")
	}
}

class Dog : Animal {
	constructor(color: String, maxAge: Int) : super(color, maxAge) {

	}

	override fun sound() {
		super.sound()
		println("Dogs barks")
	}
}
Output:
Dog color: Black, max age of dog: 10
Animal sounds: Dogs barks