Thursday, 18 January 2018

Kotlin: Object Expressions

If you want to create an object that is slight modification to the existing class, you can use anonymous class.

By using Object expressions, you can create anonymous class.

For example,
open class PrintUtil {
         public open fun welcomeMessage(name: String) {
                 println("Hello $name")
         }
}

Now we can define anonymous class like below.

         /* Define object using anonymous class */
         var anonymousObj: PrintUtil = object : PrintUtil() {

                 override fun welcomeMessage(name: String) {
                          println("Welcome $name")
                 }
         }

As you observe above snippet, I defined the object 'anonymousObj' using object expression. Object expressions are defined using the keyword 'object'.

HelloWorld.kt
open class PrintUtil {
 public open fun welcomeMessage(name: String) {
  println("Hello $name")
 }
}

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

 obj.welcomeMessage("Krishna")

 /* Define object using anonymous class */
 var anonymousObj: PrintUtil = object : PrintUtil() {

  override fun welcomeMessage(name: String) {
   println("Welcome $name")
  }
 }

 anonymousObj.welcomeMessage("Krishna")
}

Output

Hello Krishna
Welcome Krishna

You can also pass parameterized constructor while defining anonymous class

open class PrintUtil(var greeting : String) {
         public open fun welcomeMessage(name: String) {
                 println("Hello $name")
         }
}

We can define anonymous class like below.

         /* Define object using anonymous class */
         var anonymousObj: PrintUtil = object : PrintUtil("Good Morning") {

                 override fun welcomeMessage(name: String) {
                          println("Welcome $name")
                 }
         }

Find the below working application.


HelloWorld.kt

open class PrintUtil(var greeting: String) {
 public open fun welcomeMessage(name: String) {
  println("Hello $name")
 }
}

fun main(args: Array<String>) {
 var obj = PrintUtil("Good Morning")

 obj.welcomeMessage("Krishna")

 /* Define object using anonymous class */
 var anonymousObj: PrintUtil = object : PrintUtil("Good Morning") {

  override fun welcomeMessage(name: String) {
   println("Welcome $name")
  }
 }

 anonymousObj.welcomeMessage("Krishna")
}

Can I define an anonymous object without any super type?
Yes, you can define anonymous object, without any super type.

Ex
         val newAnonymousObj = object {
                 var x: Int = 1
                 var y: Int = 2
                 var z: Int = 3

                 fun welcomeUser(name: String) {
                          println("Hello $name")
                 }
         }

Find below working application.


HelloWorld.kt
fun main(args: Array<String>) {
 val newAnonymousObj = object {
  var x: Int = 1
  var y: Int = 2
  var z: Int = 3

  fun welcomeUser(name: String) {
   println("Hello $name")
  }
 }

 newAnonymousObj.welcomeUser("Krishna")

 println("Value of x is : ${newAnonymousObj.x}")
 println("Value of y is : ${newAnonymousObj.y}")
 println("Value of z is : ${newAnonymousObj.z}")
}


Output
Hello Krishna
Value of x is : 1
Value of y is : 2
Value of z is : 3

Can an object expression access the code of enclosing block?
Yes, an object expression can access the code of enclosing block.


HelloWorld.kt

fun main(args: Array<String>) {
 var message = "Hello, Good Morning"
 
 val newAnonymousObj = object {

  fun welcomeUser(name: String) {
   println("$message $name")
  }
 }

 newAnonymousObj.welcomeUser("Krishna")

}

Output

Hello, Good Morning Krishna



Previous                                                 Next                                                 Home

No comments:

Post a Comment