Kotlin – Inheritance

Kotlin – Inheritance

In this part, we will find out about legacy. By definition, we as a whole realize that legacy implies gathering a few properties of the mother class into the kid class. In Kotlin, the base class is named as “Any”, which is the super class of the ‘any’ default class announced in Kotlin. Like all different OOPS, Kotlin additionally gives this usefulness utilizing one watchword known as “:”.

Everything in Kotlin is of course last, thus, we have to utilize the catchphrase “open” before the class revelation to make it admissible to acquire. Investigate the accompanying case of legacy.

import java.util.Arrays
open class ABC{
fun think (){
print("Hey!! softwarelogy best! ")
}
}
class BCD: ABC(){ // inheritence happend using default constructor
}
fun main(args: Array<String>) {
var a=BCD()
a.think()
}

The above bit of code will yield the accompanying yield in the program.

Hey!! softwarelogy best!

Presently, imagine a scenario in which we need to supersede the think() strategy in the tyke class. At that point, we have to consider the accompanying model where we are making two classes and abrogate one of its capacity into the youngster class.

import java.util.Arrays
open class ABC{
open fun think (){
print("Hey!! softwarelogy best! ")
}
}
class BCD: ABC(){ // inheritance happens using default constructor
override fun think(){
print("Hellooo worldd")
}
}
fun main(args: Array<String>) {
var a=BCD()
a.think()
}

The above bit of code will call the youngster class acquired technique and it will yield the accompanying yield in the program. Like Java, Kotlin too doesn’t permit different legacies.

Hey!! softwarelogy best!

Leave a Reply

Your email address will not be published. Required fields are marked *