ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

Kotlin program | Example of Properties Getter and Setter Methods

Kotlin | Properties Getter and Setter Methods: Here, we are implementing a Kotlin program to demonstrate the example of properties getter and setter methods.
Submitted by IncludeHelp, on June 12, 2020

Properties Getter and Setter Methods

  • Variable having a class-level scope, declared inside the class body but outside the functions called property.
  • Property can be declared with var(mutable) and val (read-only).
    var/val <propertyName>: <PropertyType> = <property_initializer>
    [<getter>]
    [<setter>]
    
  • property_initializer, getter, and Setter are optional.
  • Getter and Setter Auto-Generated into the code.
  • Getter is used to get the value of properties and setter is used to set value of properties.
  • val(read-only) type property does not allow setter.
  • If we don't want public access of setter than declare it private.
    var name:String
    private set
    

Program to demonstrate the example of Properties Getter and Setter Methods in Kotlin

package com.includehelp

// Declare class,
class America{
    // Declare property with initial value
    var city:String = "NewYork"
    // Auto Generated getter and setter
}

// Declare class,
class India{
    // Declare property with initial value
    var city:String = "Delhi"

    // define optional getter and setter
    get() = field // Getter
    set(value) {  // Setter
        field=value
    }
}

// Declare class, define optional getter and setter
class China{
    // Declare property with initial value
    var city:String = "Wuhan"

    // private setter, cant set value from outside the class
    private set

    // member function to set property
    fun setCity(city:String){
        this.city=city
    }

}

// declare class, with customized getter and setter
class Japan{
    // Declare property with initial value
    var city:String = "Tokyo"

    // Getter of property
    get() = field.toUpperCase()

    //setter of Property
    set(value) {
        field="Modern City $value"
    }
}

// Main function, entry Point of Program
fun main(){
    // create Instance
    val america=America()
    america.city="Alsakaaa" // access setter
    println("America : ${america.city}") // access getter

    // create Instance
    val india=India()
    india.city="Mumbai" // access setter
    println("India : ${india.city}") // access getter

    // create Instance
    val china=China()
    // Try to access private setter, leads to compile time error
    // china.city="Beijing"

    // Set City by calling member function
    china.setCity("Beijing")
    println("China : ${china.city}") // access getter

    // create Instance
    val japan=Japan()
    india.city="Quoto" // access setter
    println("Japan : ${india.city}") // access getter
}

Output:

America : Alsakaaa
India : Mumbai
China : Beijing
Japan : Quoto
ADVERTISEMENT



ADVERTISEMENT



Comments and Discussions

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.