Monday, 8 January 2018

Kotlin: Calling super class methods

By using super keyword, you can call super class methods.

Syntax
super.methodName

SuperDemo.kt

open class BaseClass {
 open fun printMsg() {
  println("Hello Base class")
 }
}

open class DerivedClass : BaseClass() {
 override fun printMsg() {
  println("Hello Derived class")
  super.printMsg()
 }
}

fun main(args: Array<String>) {
 var obj = DerivedClass()

 obj.printMsg()
}

Output

Hello Derived class
Hello Base class


Previous                                                 Next                                                 Home

No comments:

Post a Comment