Getters and Setters with their visibility mode in Kotlin Programming language

Unlike java, kotlin provides default implementation of getters and setters for member variables. We don’t need to manually write them. For example

class Human {
var name: String = "John"

}
fun main() {
var human = Human()
println(animal.name)
animal.name= "Joy"

}

Here, we can access member variables with dot operator on its object and we can also assign value to it without getters and setters. But if we want to have custom implementation of  getters and setters, kotlin has support for it as well. Let’s look at how we declare variable in kotlin to get deeper understanding.

This is a syntax in kotlin to declare a variable

var <propertyName> [: <PropertyType>] [= <property_initializer>]
    [<getter>]
    [<setter>]

For instance:

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

Here, components inside big bracket [ ] are optional. Thus,

var name = "John" 
lateinit var name :String
var name = "John" 
         get() = "Hi $name"
all are valid syntax.

We are going to talk about lateinit in later post.  Here is basic class called Animal and it has two member variables name and message. Member variable, message has getter and setter. It has a default getter implementation whereas setter has customer implementation. Field simply refers to the message variable. If you see field variable in getters and setters , then just remember that it is referring to its member variable.

class Human {

var name: String = "John"
var message: String = "Hi, how are you ? "

get() = field
set(value) {
field = "$message $name and $value"
}

}

fun main() {
var human = Human()

println(animal.name)
println(animal.message)

animal.message = "Joy"
println(animal.message)

}

It’s output

John
Hi, how are you ?
Hi, how are you ? John and Joy

In kotlin, by default, member variable is public if we don’t specify visibility modifier and by default getter and setter have the same visibility modifier to that of member variable.  For example

var name: String = "John" // public by default if we don't specify modifier
get () = field // public because name has public modifer and no expicitly modifier is given to it

private var message: String = "Hi, how are you ? " // private modifier
get() = field // private because message has private modifier and no expicitly modifier is given to it
private set(value) { // private for same above reason
field = "$message $name and $value"
}
Additional rules about modifiers with getters and setters 

 

  1. We can’t change getter visibility modifier to other modifier. It has to exactly same as that of member variable. For example
public var message: String = "Hi, how are you ? "
private get() = field // This will give you error, it needs to be public

      2.  However, we can have different modifiers in setter to that of member variable modifier. But it needs to be less permissive. 

protected var message: String = "Hi, how are you ? "
protected get() = field // Here getter need to be protected according to 1 rule
public set(value) { // This will give error beacause public has greater premissive than protected
field = "$message $name and $value"
}
private set(value) { // This is correct beacause private has less premissive than protected
field = "$message $name and $value"
}
public set(value) { // It is protected beacause no explicit modifier is added (
field = "$message $name and $value"
}

Leave a Reply

Your email address will not be published. Required fields are marked *