Getters/setters

In general property in Kotlin consits from backfield, that hold data in memory, and getter/setter methods. Suppose property has name result, then Kotlin will generate methods getResult() and setResult(v).

On JVM: access to private properties with default getters and setters is optimized to avoid function call overhead.

You can change visibility of getter/setter method. For example, make setter private means, that user can read value from property, but can not modify.

You can override getter/setter methods.

The field variable within getter/setter refers to the default backfield of property. You can define custom backfield.

class Cls{
    open var parentProp = ""
}

class MyClass: Cls() {

    var prop: String = ""
        private set


    var prop1: String = ""
        set(v) {
            //... do something
            field = v
        }

    private var _myProp = "" // custom backfield for myProp2 property

    var myProp: String
        get() {
            // ... do something
            return _myProp
        }
        set(v) {
            // ... do something
            _myProp = v
        }
        
     // override property from parent class   
     override var parentProp: String=""
         get() = super.parentProp
         set(v) {
             // ... do somethinf
             field = v
         } 
}