January 2017
Intermediate to advanced
420 pages
9h 25m
English
The visibility access rules we have discussed for fields apply to properties as well. Therefore, you can have private, protected, or public (default) properties. Furthermore, the setter can have different, more restrictive visibility than the getter (the getter code is generated for you automatically in the following case):
class WithPrivateSetter(property: Int) {
var SomeProperty: Int = 0
private set(value) {
field = value
}
init {
SomeProperty = property
}
}
val withPrivateSetter = WithPrivateSetter(10)
println("withPrivateSetter:${withPrivateSetter.SomeProperty}")
There are scenarios when properties are subject to class inheritance. If this happens, typically protected visibility, at least for the setter, is more appropriate:
open ...
Read now
Unlock full access