Class properties

In Kotlin, each class property is considered a first-class citizen, which we discussed in the Properties – A first class citizen section of Chapter 2, Introduction to Object-Oriented Programming. In this section, we will learn more about properties and how to define the getter and setter functions that are explicitly provided by Kotlin. Let's create a Person class with two properties, name and age:

class Person {
var name: String = ""
var age : Int = 0
}

Create an instance of the Person class. Assign some values to each property and display the values on the screen:

fun main(args: Array<String>) {
val abid = Person()

abid.name = "Abid Khan"
abid.age = 40

println(abid.name)
println(abid.age)
}

When we assign a value to a class property, such as abid.name = "Abid Khan", or get a value from the property, such as println(abid.name), we are not actually reading or writing the property directly. Instead, Kotlin compiles this code in Java and provides the getter and setter methods under the hood. When we write abid.age = 40, Kotlin calls the setAge() function to set or update the value. We can verify this by using decompiled Java code. In the IntelliJ Idea menu, click on Tools | Kotlin | Show Kotlin Byte code and press Decompile. The following is the Kotlin-generated Java code for the Person class. Each property in Kotlin represents its own getter and setter functions:

public final class Person {
@NotNull
private String name = "";
private int age;

@NotNull
public final String getName() {
return this.name;
}

public final void setName(@NotNull String var1) {
Intrinsics.checkParameterIsNotNull(var1, "<set-?>");
this.name = var1;
}

public final int getAge() {
return this.age;
}

public final void setAge(int var1) {
this.age = var1;
}
}

These are Java methods that are used to access the properties. To override these methods and write our functions, Kotlin provides each function with two fields. All properties of the Kotlin classes contain get() and set() functions for reading and writing purposes, and they each contain two built-in backing fields as well, which are value and field. Take a look at the following example:

class Person {
var name: String = ""
get() = field
set(value) {
field = value
}

var age : Int = 0
get() = field
set(update) {
if(update > 0)
field = update
}
}

To read or access the property, we need to declare the get function right after the property declaration and use the implicit backing field that comes with each property and stores the actual data. We can access this backing field using the field keyword. Similarly, to set or write class properties, declare the set function either right after the property or after the get function. The set function takes one parameter, value, to update the backing field. The parameter value in the set function is not a fixed keyword; it can be replaced with something else. In the set function, we can verify the value before assigning it to the property as we did previously. We will update the name property if the received value is not empty, and update the age if the received value is positive:

fun main(args: Array<String>) {
val abid = Person()
abid.name = "Abid Khan"
abid.age = 40

println(abid.name)
println(abid.age)

abid.name = ""
abid.age = 0

println(abid.name)
println(abid.age)
}

To verify the set function, try to assign name and age properties with an invalid input; for example, a negative value for the age or an empty string for the name.

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

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