Friday, 19 January 2018

Kotlin: Anonymous Inner class

Anonymous inner classes can be created using object expressions.

Ex
         printMessage(object : PrinterUtil() {
                 override fun welcomeMessage() {
                          println("Good Morning customer")
                 }
         })

Find the below working application.

HelloWorld.kt
open class PrinterUtil {
 open fun welcomeMessage() {
  println("Welcome user")
 }
}

fun printMessage(printUtl: PrinterUtil) {
 printUtl.welcomeMessage()
}

fun main(args: Array<String>) {
 printMessage(object : PrinterUtil() {
  override fun welcomeMessage() {
   println("Good Morning customer")
  }
 })
}

Output
Good Morning customer



Previous                                                 Next                                                 Home

No comments:

Post a Comment