Site icon C1CTech

Kotlin Visibility Modifiers

&NewLine;<p>This post is about&nbsp&semi;<span style&equals;"color&colon; &num;0000ff&semi;"><strong>Access&sol;Visibility modifiers in kotlin<&sol;strong><&sol;span>&nbsp&semi;and how to use them&period;<&sol;p>&NewLine;&NewLine;&NewLine;&NewLine;<figure class&equals;"wp-block-embed is-type-video is-provider-youtube wp-block-embed-youtube wp-embed-aspect-4-3 wp-has-aspect-ratio"><div class&equals;"wp-block-embed&lowbar;&lowbar;wrapper">&NewLine;<amp-youtube layout&equals;"responsive" width&equals;"1200" height&equals;"900" data-videoid&equals;"ztCWVsSccWY" title&equals;"Kotlin Visibility Modifiers"><a placeholder href&equals;"https&colon;&sol;&sol;youtu&period;be&sol;ztCWVsSccWY"><img src&equals;"https&colon;&sol;&sol;i&period;ytimg&period;com&sol;vi&sol;ztCWVsSccWY&sol;hqdefault&period;jpg" layout&equals;"fill" object-fit&equals;"cover" alt&equals;"Kotlin Visibility Modifiers"><&sol;a><&sol;amp-youtube>&NewLine;<&sol;div><&sol;figure>&NewLine;&NewLine;&NewLine;&NewLine;<h3 class&equals;"wp-block-heading"><span style&equals;"color&colon; &num;000080&semi;"><strong><span class&equals;"has-inline-color">Access Modifiers<&sol;span><&sol;strong><&sol;span><&sol;h3>&NewLine;&NewLine;&NewLine;&NewLine;<p class&equals;"p1">In Kotlin&comma;&nbsp&semi;<span style&equals;"color&colon; &num;008000&semi;"><b>visibility modifiers<&sol;b><&sol;span> are used to restrict the accessibility of Classes&comma; objects&comma; interfaces&comma; constructors&comma; functions&comma; properties and their setters to a certain level&period; Getters always have the same visibility as the property&period;<&sol;p>&NewLine;&NewLine;&NewLine;&NewLine;<p>There are four visibility modifiers in Kotlin&colon;<&sol;p>&NewLine;&NewLine;&NewLine;&NewLine;<ul class&equals;"ul1 wp-block-list"><li class&equals;"li1"><strong><span style&equals;"color&colon; &num;0000ff&semi;">public<&sol;span><&sol;strong><&sol;li><li class&equals;"li1"><strong><span style&equals;"color&colon; &num;0000ff&semi;">protected<&sol;span><&sol;strong><&sol;li><li class&equals;"li1"><strong><span style&equals;"color&colon; &num;0000ff&semi;">internal<&sol;span><&sol;strong><&sol;li><li class&equals;"li1"><strong><span style&equals;"color&colon; &num;0000ff&semi;">private<&sol;span><&sol;strong><&sol;li><&sol;ul>&NewLine;&NewLine;&NewLine;&NewLine;<p><span style&equals;"color&colon; &num;000080&semi;"><strong>Note&colon;<&sol;strong>&nbsp&semi;<&sol;span>If visibility modifier is not specified&comma; it is <span style&equals;"color&colon; &num;008000&semi;"><strong>public<&sol;strong><&sol;span> by default&period;<&sol;p>&NewLine;&NewLine;&NewLine;&NewLine;<h3 class&equals;"p1 wp-block-heading"><span style&equals;"color&colon; &num;000080&semi;"><b>Visibility Modifiers Inside Package<&sol;b><&sol;span><&sol;h3>&NewLine;&NewLine;&NewLine;&NewLine;<ul class&equals;"wp-block-list"><li><span style&equals;"color&colon; &num;0000ff&semi;"><strong><span class&equals;"has-inline-color">public<&sol;span><&sol;strong><&sol;span> &colon; declarations will be visible everywhere&period;<&sol;li><li><span style&equals;"color&colon; &num;0000ff&semi;"><strong><span class&equals;"has-inline-color">Private<&sol;span><&sol;strong><&sol;span>&colon; visible inside the file containing the declaration&period;<&sol;li><li><span style&equals;"color&colon; &num;0000ff&semi;"><strong><span class&equals;"has-inline-color">internal<&sol;span><&sol;strong><&sol;span>&colon; visible inside the same module&period;<&sol;li><li><span style&equals;"color&colon; &num;0000ff&semi;"><strong><span class&equals;"has-inline-color">Protected<&sol;span><&sol;strong><&sol;span>&colon; not available for top-level declarations&period;<&sol;li><&sol;ul>&NewLine;&NewLine;&NewLine;&NewLine;<p><span style&equals;"color&colon; &num;000080&semi;"><strong>Example<&sol;strong><&sol;span><&sol;p>&NewLine;&NewLine;&NewLine;&NewLine;<pre class&equals;"wp-block-preformatted"><span style&equals;"color&colon; &num;0000ff&semi;"><strong>&sol;&sol; file name&colon; KotlinTestFile&period;kt<&sol;strong><&sol;span>&NewLine;&NewLine;package com&period;mytest&NewLine;&NewLine;var age &equals; 25 <strong><span style&equals;"color&colon; &num;008000&semi;">&sol;&sol; public by default and visible everywhere<&sol;span><&sol;strong>&NewLine;&NewLine;private var mobileNumber &equals; 123456 <strong><span style&equals;"color&colon; &num;008000&semi;">&sol;&sol; visible inside KotlinTestFile&period;kt<&sol;span><&sol;strong>&NewLine;&NewLine;internal var companyName &equals; "MyCompany" <strong><span style&equals;"color&colon; &num;008000&semi;">&sol;&sol; visible inside the same module<&sol;span><&sol;strong>&NewLine;&NewLine;<span style&equals;"color&colon; &num;0000ff&semi;"><strong>&sol;&sol;error&colon; cannot create protected functions&sol;properties in kotlin file&NewLine;&sol;&sol;protected var id &equals; 12345678<&sol;strong><&sol;span>&NewLine;&NewLine;var name &equals; "John" <span style&equals;"color&colon; &num;008000&semi;"><strong>&sol;&sol; public by default and visible everywhere<&sol;strong><&sol;span>&NewLine; get&lpar;&rpar; &equals; field <strong><span style&equals;"color&colon; &num;008000&semi;">&sol;&sol; visibility same as its property<&sol;span><&sol;strong>&NewLine; private set&lpar;value&rpar; &lbrace; <strong><span style&equals;"color&colon; &num;008000&semi;">&sol;&sol; visible inside KotlinTestFile&period;kt<&sol;span><&sol;strong>&NewLine; field &equals; value&NewLine; &rcub;&NewLine;&NewLine;private class classA &lbrace;&rcub; <span style&equals;"color&colon; &num;008000&semi;"><strong>&sol;&sol; visible inside KotlinTestFile&period;kt<&sol;strong><&sol;span><&sol;pre>&NewLine;&NewLine;&NewLine;&NewLine;<h3 class&equals;"p1 wp-block-heading"><span style&equals;"color&colon; &num;000080&semi;"><b>Visibility Modifiers Inside Classes and Interfaces<&sol;b><&sol;span><&sol;h3>&NewLine;&NewLine;&NewLine;&NewLine;<p>Visibility modifiers for members &lpar;functions&comma; properties&rpar; declared inside a class&colon;<&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;&NewLine;&NewLine;&NewLine;<ul class&equals;"wp-block-list"><li><span style&equals;"color&colon; &num;0000ff&semi;"><strong><span class&equals;"has-inline-color">public<&sol;span><&sol;strong><&sol;span> &colon; visible to any client who can see the declaring class&period;<&sol;li><li><span style&equals;"color&colon; &num;0000ff&semi;"><strong><span class&equals;"has-inline-color">Private<&sol;span><&sol;strong><&sol;span>&colon; visible inside the class only&period;<&sol;li><li><span style&equals;"color&colon; &num;0000ff&semi;"><strong><span class&equals;"has-inline-color">internal<&sol;span><&sol;strong><&sol;span>&colon; visible to any client inside the module that can see the declaring class&period;<&sol;li><li><span style&equals;"color&colon; &num;0000ff&semi;"><strong><span class&equals;"has-inline-color">Protected<&sol;span><&sol;strong><&sol;span>&colon; visible inside the class and its subclasses&period;<&sol;li><&sol;ul>&NewLine;&NewLine;&NewLine;&NewLine;<p class&equals;"p1"><span style&equals;"color&colon; &num;000080&semi;"><b>Note&colon;<&sol;b><&sol;span>&nbsp&semi;If you override a&nbsp&semi;protected&nbsp&semi;member in the derived class without specifying its visibility&comma; its visibility will also be&nbsp&semi;<span style&equals;"color&colon; &num;008000&semi;"><strong>protected<&sol;strong><&sol;span>&period;<&sol;p>&NewLine;&NewLine;&NewLine;&NewLine;<p><span style&equals;"color&colon; &num;000080&semi;"><strong>Example<&sol;strong><&sol;span><&sol;p>&NewLine;&NewLine;&NewLine;&NewLine;<pre class&equals;"wp-block-preformatted">package com&period;mytest&NewLine;&NewLine;open class Test01&lpar;&rpar; &lbrace;&NewLine;&NewLine; <span style&equals;"color&colon; &num;008000&semi;"><strong>&sol;&sol; private to Test01&lpar;Base class&rpar;<&sol;strong><&sol;span>&NewLine; private fun privateMethod&lpar;&rpar; &lbrace;&NewLine; println&lpar;"private method call"&rpar;&NewLine; &rcub;&NewLine;&NewLine; <strong><span style&equals;"color&colon; &num;008000&semi;">&sol;&sol; public by default and visible everywhere<&sol;span><&sol;strong>&NewLine; fun publicMethod&lpar;&rpar; &lbrace;&NewLine; privateMethod&lpar;&rpar;&NewLine; println&lpar;"public method call"&rpar;&NewLine; &rcub;&NewLine;&NewLine;<strong><span style&equals;"color&colon; &num;008000&semi;"> &sol;&sol; visible to Test01&lpar;Base class&rpar; and Test02&lpar;Derived class&rpar;&NewLine;<&sol;span><&sol;strong> protected fun protectedMethod&lpar;&rpar; &lbrace;&NewLine; println&lpar;"protected method call"&rpar;&NewLine; &rcub;&NewLine;&NewLine; <strong><span style&equals;"color&colon; &num;008000&semi;"> &sol;&sol; visible inside the same module<&sol;span><&sol;strong>&NewLine; internal fun internalMethod&lpar;&rpar; &lbrace;&NewLine; println&lpar;"internal method call"&rpar;&NewLine; &rcub;&NewLine;&rcub;&NewLine;&NewLine;fun main&lpar;&rpar; &lbrace;&NewLine;<span style&equals;"color&colon; &num;008000&semi;"><strong>&sol;&sol;creating Test01 instance<&sol;strong><&sol;span>&NewLine; val test01 &equals; Test01&lpar;&rpar;&NewLine;&NewLine;<strong><span style&equals;"color&colon; &num;008000&semi;"> &sol;&sol;publicMethod&lpar;&rpar; and internalMethod&lpar;&rpar; of Test01 class are visible&NewLine;<&sol;span><&sol;strong> test01&period;publicMethod&lpar;&rpar;&NewLine; test01&period;internalMethod&lpar;&rpar;&NewLine;&NewLine;<span style&equals;"color&colon; &num;0000ff&semi;"><strong> &sol;&sol;compile error&colon; protectedMethod&lpar;&rpar; and privateMethod&lpar;&rpar; of Test01 class are not visible&NewLine;<&sol;strong>&NewLine;<strong> &sol;&sol;test01&period;protectedMethod&lpar;&rpar; <span style&equals;"color&colon; &num;008000&semi;"> &sol;&sol;accessible in subclasses only&NewLine;<&sol;span><&sol;strong>&NewLine;<strong> &sol;&sol;test01&period;privateMethod&lpar;&rpar; <span style&equals;"color&colon; &num;008000&semi;">&sol;&sol;accessible only inside the class Test01&NewLine;<&sol;span><&sol;strong><&sol;span>&NewLine;&rcub;&NewLine;&NewLine;<span style&equals;"color&colon; &num;008000&semi;"><strong>&sol;&sol; Test02 inheriting class Test01<&sol;strong><&sol;span>&NewLine;class Test02 &colon; Test01&lpar;&rpar; &lbrace;&NewLine; fun newMethod&lpar;&rpar; &lbrace;&NewLine; <strong><span style&equals;"color&colon; &num;008000&semi;">&sol;&sol; protectedMethod&lpar;&rpar; &comma;publicMethod&lpar;&rpar; &comma;internalMethod&lpar;&rpar; of the Test01 class are visible&NewLine; &sol;&sol; privateMethod&lpar;&rpar; is not visible<&sol;span><&sol;strong>&NewLine; protectedMethod&lpar;&rpar;&NewLine;&NewLine; publicMethod&lpar;&rpar;&NewLine;&NewLine; internalMethod&lpar;&rpar;&NewLine;&NewLine; <strong><span style&equals;"color&colon; &num;008000&semi;"> &sol;&sol;privateMethod&lpar;&rpar; &sol;&sol; compile error<&sol;span><&sol;strong>&NewLine;&NewLine; &rcub;&NewLine;&rcub;<&sol;pre>&NewLine;&NewLine;&NewLine;&NewLine;<h3 class&equals;"p1 wp-block-heading"><span style&equals;"color&colon; &num;000080&semi;"><b>Changing Visibility of a Constructor<&sol;b><&sol;span><&sol;h3>&NewLine;&NewLine;&NewLine;&NewLine;<p class&equals;"p1">By default&comma; the visibility of a&nbsp&semi;constructor is&nbsp&semi;<span style&equals;"color&colon; &num;008000&semi;"><strong>public<&sol;strong><&sol;span>&period; However&comma; you can change it&period; For that&comma; you need to explicitly add&nbsp&semi;<strong><span style&equals;"color&colon; &num;0000ff&semi;">constructor<&sol;span><&sol;strong>&nbsp&semi;keyword&period;<&sol;p>&NewLine;&NewLine;&NewLine;&NewLine;<p class&equals;"p1">The constructor is&nbsp&semi;<span style&equals;"color&colon; &num;008000&semi;"><strong>public<&sol;strong><&sol;span>&nbsp&semi;by default in the example below&colon;<&sol;p>&NewLine;&NewLine;&NewLine;&NewLine;<pre class&equals;"wp-block-preformatted">class Test&lpar;val a&colon; Int&rpar; &lbrace;&NewLine; <strong><span style&equals;"color&colon; &num;008000&semi;"> &sol;&sol; code<&sol;span><&sol;strong>&NewLine;&rcub;&NewLine;&NewLine;<strong><span style&equals;"color&colon; &num;0000ff&semi;">OR<&sol;span> &NewLine;<&sol;strong>&NewLine;<span style&equals;"color&colon; &num;008000&semi;"><strong>&sol;&sol;by default the constructor is public<&sol;strong><&sol;span>&NewLine;class Test constructor&lpar;val a&colon; Int&rpar; &lbrace;&NewLine; <strong><span style&equals;"color&colon; &num;008000&semi;"> &sol;&sol; code<&sol;span><&sol;strong> &NewLine;&rcub;<&sol;pre>&NewLine;&NewLine;&NewLine;&NewLine;<p class&equals;"p1">Here&&num;8217&semi;s how you can change its visibility&period; Here the constructor is <span style&equals;"color&colon; &num;008000&semi;"><strong>private<&sol;strong><&sol;span>&period;<&sol;p>&NewLine;&NewLine;&NewLine;&NewLine;<pre class&equals;"wp-block-preformatted">class Test <strong><span style&equals;"color&colon; &num;0000ff&semi;">private<&sol;span><&sol;strong> constructor&lpar;val a&colon; Int&rpar; &lbrace;&NewLine; <strong><span style&equals;"color&colon; &num;008000&semi;">&sol;&sol; code<&sol;span><&sol;strong>&NewLine;&rcub;<&sol;pre>&NewLine;&NewLine;&NewLine;&NewLine;<p><span style&equals;"color&colon; &num;000080&semi;"><strong>Note&colon;<&sol;strong><&sol;span>&nbsp&semi;In Kotlin&comma; local functions&comma; variables and classes cannot have visibility modifiers&period;&NewLine;

Exit mobile version