Site icon C1CTech

Kotlin Getters and Setters

<p>This tutorial is about how to use getters and setters in Kotlin with the help of simple example&period;<&sol;p>&NewLine;<h3 class&equals;"p2"><span style&equals;"color&colon; &num;000080&semi;"><b>Getters and Setters<&sol;b><&sol;span><&sol;h3>&NewLine;<p>Getters are used for getting value of the property and Setters are used for setting the value of the property&period;<&sol;p>&NewLine;<p>Setters can have <strong><span style&equals;"color&colon; &num;000080&semi;">visibility modifiers <&sol;span><&sol;strong><span style&equals;"color&colon; &num;000000&semi;">but <&sol;span>Getters always have the same visibility as the property&period;<&sol;p>&NewLine;<p class&equals;"p3">Getter and Setter are the functions that are <span style&equals;"color&colon; &num;0000ff&semi;"><b>generated by default<&sol;b><&sol;span> for each class property by Kotlin&period;<&sol;p>&NewLine;<p><span style&equals;"color&colon; &num;0000ff&semi;"><strong>Example&colon;<&sol;strong><&sol;span><&sol;p>&NewLine;<p>The following code in Kotlin<&sol;p>&NewLine;<pre>class Employee &lbrace;&NewLine; var name&colon; String &equals; ""&NewLine; val age&colon; Int &equals; 28&NewLine;&rcub;<&sol;pre>&NewLine;<p>is equivalent to<&sol;p>&NewLine;<pre><span style&equals;"color&colon; &num;008000&semi;"><strong>&sol;&sol;Default get&lpar;&rpar; and set&lpar;&rpar; functions look like&colon;<&sol;strong><&sol;span>&NewLine;class Employee &lbrace;&NewLine; <strong><span style&equals;"color&colon; &num;0000ff&semi;">var<&sol;span><&sol;strong> name&colon; String &equals; ""&NewLine; get&lpar;&rpar; &equals; field&NewLine; set&lpar;value&rpar; &lbrace;&NewLine; field &equals; value&NewLine; &rcub;&NewLine; <span style&equals;"color&colon; &num;0000ff&semi;"><strong>val<&sol;strong><&sol;span> age&colon; Int &equals; 28&NewLine; get&lpar;&rpar; &equals; field&NewLine;&rcub;<&sol;pre>&NewLine;<ul>&NewLine;<li>Properties declared with <span style&equals;"color&colon; &num;0000ff&semi;"><strong>var<&sol;strong><&sol;span> have both <span style&equals;"color&colon; &num;008000&semi;"><strong>get&lpar;&rpar;<&sol;strong><&sol;span> and <strong><span style&equals;"color&colon; &num;008000&semi;">set&lpar;&rpar;<&sol;span><&sol;strong> functions&period;<&sol;li>&NewLine;<li>But the properties declared with <strong><span style&equals;"color&colon; &num;0000ff&semi;">val<&sol;span><&sol;strong> have only <strong><span style&equals;"color&colon; &num;008000&semi;">get&lpar;&rpar;<&sol;span><&sol;strong> function&period; This is because values to <span style&equals;"color&colon; &num;008000&semi;"><strong>val<&sol;strong><&sol;span> cannot be reassigned&period;<&sol;li>&NewLine;<li>The <strong><span style&equals;"color&colon; &num;000080&semi;">field<&sol;span><&sol;strong> keyword is used to represent the variable and the <strong><span style&equals;"color&colon; &num;000080&semi;">value<&sol;span><&sol;strong> is used to represent the value to be assigned to the variable&lpar; It can be changed also&rpar;&period;<&sol;li>&NewLine;<li>These <span style&equals;"color&colon; &num;008000&semi;"><strong>get&lpar;&rpar; and set&lpar;&rpar; <&sol;strong><&sol;span>functions are redundant as Kotlin provides them by default&period;<&sol;li>&NewLine;<&sol;ul>&NewLine;<p class&equals;"p1">When you instantiate object of the <span style&equals;"color&colon; &num;008000&semi;"><strong>Employee<&sol;strong><&sol;span> class and initialize the <span style&equals;"color&colon; &num;008000&semi;"><strong>name<&sol;strong><&sol;span> property&comma; it is passed to the setters parameter <strong><span style&equals;"color&colon; &num;0000ff&semi;">value<&sol;span><&sol;strong> and sets <span style&equals;"color&colon; &num;0000ff&semi;"><strong>field<&sol;strong><&sol;span> to <span style&equals;"color&colon; &num;008000&semi;"><strong>value<&sol;strong><&sol;span>&period;<&sol;p>&NewLine;<&excl;-- WP QUADS Content Ad Plugin v&period; 2&period;0&period;98&period;1 -->&NewLine;<div class&equals;"quads-location quads-ad2" id&equals;"quads-ad2" style&equals;"float&colon;none&semi;margin&colon;0px&semi;">&NewLine;&NewLine;<&sol;div>&NewLine;&NewLine;<pre>val employee &equals; Employee&lpar;&rpar;&NewLine;employee&period;name &equals; "Arun" <span style&equals;"color&colon; &num;008000&semi;"><strong>&sol;&sol; calls set&lpar;&rpar; function<&sol;strong><&sol;span><&sol;pre>&NewLine;<p class&equals;"p1">Now&comma; when you access properties &lpar;name&comma; age&rpar; of the object&comma; you will get <span style&equals;"color&colon; &num;008000&semi;"><strong>field<&sol;strong><&sol;span> because of the code <span style&equals;"color&colon; &num;0000ff&semi;"><strong>get&lpar;&rpar; &equals; field<&sol;strong><&sol;span>&period;<&sol;p>&NewLine;<pre>println&lpar;"The age of &dollar;&lbrace;employee&period;name&rcub; is &dollar;&lbrace;employee&period;age&rcub;"&rpar;<&sol;pre>&NewLine;<p><span style&equals;"color&colon; &num;0000ff&semi;"><strong>Output&colon;<&sol;strong><&sol;span><&sol;p>&NewLine;<pre>The age of Arun is 28<&sol;pre>&NewLine;<h3 class&equals;"p1"><span style&equals;"color&colon; &num;000080&semi;"><strong>Custom Getters and Setters<&sol;strong><&sol;span><&sol;h3>&NewLine;<p class&equals;"p1">We can also provide <span style&equals;"color&colon; &num;008000&semi;"><b>custom<&sol;b><&sol;span> get&lpar;&rpar; and set&lpar;&rpar; function&colon;<&sol;p>&NewLine;<p><strong><span style&equals;"color&colon; &num;0000ff&semi;">Example&colon;<&sol;span><&sol;strong><&sol;p>&NewLine;<pre><strong><span style&equals;"color&colon; &num;008000&semi;">&sol;&sol;custom getters and setters<&sol;span><&sol;strong>&NewLine;class Employee&lbrace;&NewLine; var name&colon; String &equals; ""&NewLine; get&lpar;&rpar; &lbrace;&NewLine; println&lpar;"get&lpar;&rpar; of name called"&rpar;&NewLine; return field&period;toString&lpar;&rpar;&NewLine; &rcub;&NewLine; set&lpar;value&rpar; &lbrace;&NewLine; println&lpar;"set&lpar;&rpar; of name called"&rpar;&NewLine; if &lpar;value&period;length &lt&semi; 3&rpar;&NewLine; field &equals; "INVALID NAME"&NewLine; else&NewLine; field &equals; value&NewLine; &rcub;&NewLine; val age&colon; Int &equals; 28&NewLine; get&lpar;&rpar;&lbrace;&NewLine; println&lpar;"get&lpar;&rpar; of age called"&rpar;&NewLine; return field&NewLine; &rcub;&NewLine;&rcub;&NewLine;&NewLine;fun main&lpar;&rpar; &lbrace;&NewLine; val employee &equals; Employee&lpar;&rpar;&NewLine; <strong><span style&equals;"color&colon; &num;008000&semi;">&sol;&sol; calls set&lpar;&rpar; function of name property<&sol;span><&sol;strong>&NewLine; employee&period;name &equals; "Arun" &NewLine;<strong><span style&equals;"color&colon; &num;008000&semi;"> &sol;&sol; calls get&lpar;&rpar; function of name and age property<&sol;span><&sol;strong>&NewLine; println&lpar;"The age of &dollar;&lbrace;employee&period;name&rcub; is &dollar;&lbrace;employee&period;age&rcub;"&rpar;&NewLine;&rcub;<&sol;pre>&NewLine;<p><span style&equals;"color&colon; &num;0000ff&semi;"><strong>Output&colon;<&sol;strong><&sol;span><&sol;p>&NewLine;<pre>set&lpar;&rpar; of name called&NewLine;get&lpar;&rpar; of name called&NewLine;get&lpar;&rpar; of age called&NewLine;The age of Arun is 28<&sol;pre>&NewLine;<ul>&NewLine;<li>The Employee class custom <span style&equals;"color&colon; &num;008000&semi;"><strong>get&lpar;&rpar;<&sol;strong><&sol;span> and <strong><span style&equals;"color&colon; &num;008000&semi;">set&lpar;&rpar;<&sol;span><&sol;strong> function is called for <span style&equals;"color&colon; &num;0000ff&semi;"><strong>name<&sol;strong><&sol;span> and custom <strong><span style&equals;"color&colon; &num;008000&semi;">get&lpar;&rpar;<&sol;span><&sol;strong> is called for <span style&equals;"color&colon; &num;0000ff&semi;"><strong>age<&sol;strong><&sol;span>&period;<&sol;li>&NewLine;<li>The <strong><span style&equals;"color&colon; &num;008000&semi;">set&lpar;&rpar;<&sol;span><&sol;strong> function is called internally to set property value using <strong><span style&equals;"color&colon; &num;008000&semi;">employee&period;name &equals; &&num;8220&semi;Arun&&num;8221&semi;&period;<&sol;span><&sol;strong><&sol;li>&NewLine;<li>The <strong><span style&equals;"color&colon; &num;008000&semi;">get&lpar;&rpar;<&sol;span><&sol;strong> function is called internally to get property value using <strong><span style&equals;"color&colon; &num;008000&semi;">employee&period;name<&sol;span><&sol;strong>&comma; <strong><span style&equals;"color&colon; &num;008000&semi;">employee&period;age&period;<&sol;span><&sol;strong><&sol;li>&NewLine;<&sol;ul>&NewLine;&NewLine;

Exit mobile version