The copy function

The copy function helps to create an independent instance by copying the existing object. This function does not exist in the normal class, which makes it a bit inconvenient when we need a new object of an existing type. Let's create a normal class and try to create a copy of existing instance:

  1. Create an object of Person and assign it to another person object:
class Person(var name : String, var age: Int, var height: Double)
val abid = Person("Abid", 40, 6.0)
val khan = abid

Ideally, both abid and khan should be two separate entities, but both objects are aliases of each other and point to the same memory location:

  1. To verify that both variables are pointing to the same memory location, perform the following actions:
  • Display the name of the first object
  • Update the name of the second object
  • Display the name of the first object

Let's implement this in the following code:

class Person(var name : String, var age: Int, var height: Double)
fun main(args: Array<String>) {

val abid = Person("Abid", 40, 6.0)
val khan = abid

println(abid.name) //Print Abid name
khan.name = "Khan" // Update Khan name
println(abid.name) // Print Abid name

if(abid == khan) {
println("Pointing to the same memory location")
}
}

When the khan object's name is updated, it will update the abid object's name and vice versa, because both instances are the same.

  1. To get a new instance, the data class provides the copy function to clone the object. Create a data class, Person, and use the copy function to create a new object:
data class Person(var name : String, var age: Int, var height: Double)
val abid = Person("Abid", 40, 6.0)
val khan = abid.copy()

The abid is an object of the Person type, and khan is another object of the Person type. Both have their own memory location. If the property of one object is updated, it won't affect the other:

println(abid) //Print Abid
khan.name = "Khan" // Update Khan name
println(abid) // Print Abid

The copy function does much more than object-cloning. When creating the object copy, we can change any attribute. For example, if we want to keep everything the same except for the user's name, we can pass the name attribute as a parameter to the copy function:

val jon = abid.copy("Jon")
println(jon)
  1. Assign values to a new object using the named parameters: 
val tom = abid.copy(name = "Tom", height = 5.11)
println(tom)

A complete code example is as follows:

data class Person(var name : String, var age: Int, var height: Double)
fun main(args: Array<String>) {

// Create new instance by using copy function
val abid = Person("Abid", 40, 6.0)
val khan = abid.copy()

println(abid) //Print Abid
khan.name = "Khan" // Update Khan name
println(abid) // Print Abid

println("Are objects pointing to the same memory locations = ${abid === khan}")

// Add new values into copied object.
val bob = abid.copy("Bob")
println(bob)

val jon = abid.copy(name = "Jon", height = 5.9)
println(jon)
}

If we verify the output of this code, we can see that the copy function helps to create a new object of Person in the memory:

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.220.152.139