<p>This post is about ;<span style="color: #0000ff;"><strong>Access/Visibility modifiers in kotlin</strong></span> ;and how to use them.</p>



<figure class="wp-block-embed is-type-video is-provider-youtube wp-block-embed-youtube wp-embed-aspect-4-3 wp-has-aspect-ratio"><div class="wp-block-embed__wrapper">
<amp-youtube layout="responsive" width="1200" height="900" data-videoid="ztCWVsSccWY" title="Kotlin Visibility Modifiers"><a placeholder href="https://youtu.be/ztCWVsSccWY"><img src="https://i.ytimg.com/vi/ztCWVsSccWY/hqdefault.jpg" layout="fill" object-fit="cover" alt="Kotlin Visibility Modifiers"></a></amp-youtube>
</div></figure>



<h3 class="wp-block-heading"><span style="color: #000080;"><strong><span class="has-inline-color">Access Modifiers</span></strong></span></h3>



<p class="p1">In Kotlin, ;<span style="color: #008000;"><b>visibility modifiers</b></span> are used to restrict the accessibility of Classes, objects, interfaces, constructors, functions, properties and their setters to a certain level. Getters always have the same visibility as the property.</p>



<p>There are four visibility modifiers in Kotlin:</p>



<ul class="ul1 wp-block-list"><li class="li1"><strong><span style="color: #0000ff;">public</span></strong></li><li class="li1"><strong><span style="color: #0000ff;">protected</span></strong></li><li class="li1"><strong><span style="color: #0000ff;">internal</span></strong></li><li class="li1"><strong><span style="color: #0000ff;">private</span></strong></li></ul>



<p><span style="color: #000080;"><strong>Note:</strong> ;</span>If visibility modifier is not specified, it is <span style="color: #008000;"><strong>public</strong></span> by default.</p>



<h3 class="p1 wp-block-heading"><span style="color: #000080;"><b>Visibility Modifiers Inside Package</b></span></h3>



<ul class="wp-block-list"><li><span style="color: #0000ff;"><strong><span class="has-inline-color">public</span></strong></span> : declarations will be visible everywhere.</li><li><span style="color: #0000ff;"><strong><span class="has-inline-color">Private</span></strong></span>: visible inside the file containing the declaration.</li><li><span style="color: #0000ff;"><strong><span class="has-inline-color">internal</span></strong></span>: visible inside the same module.</li><li><span style="color: #0000ff;"><strong><span class="has-inline-color">Protected</span></strong></span>: not available for top-level declarations.</li></ul>



<p><span style="color: #000080;"><strong>Example</strong></span></p>



<pre class="wp-block-preformatted"><span style="color: #0000ff;"><strong>// file name: KotlinTestFile.kt</strong></span>

package com.mytest

var age = 25 <strong><span style="color: #008000;">// public by default and visible everywhere</span></strong>

private var mobileNumber = 123456 <strong><span style="color: #008000;">// visible inside KotlinTestFile.kt</span></strong>

internal var companyName = "MyCompany" <strong><span style="color: #008000;">// visible inside the same module</span></strong>

<span style="color: #0000ff;"><strong>//error: cannot create protected functions/properties in kotlin file
//protected var id = 12345678</strong></span>

var name = "John" <span style="color: #008000;"><strong>// public by default and visible everywhere</strong></span>
 get() = field <strong><span style="color: #008000;">// visibility same as its property</span></strong>
 private set(value) { <strong><span style="color: #008000;">// visible inside KotlinTestFile.kt</span></strong>
 field = value
 }

private class classA {} <span style="color: #008000;"><strong>// visible inside KotlinTestFile.kt</strong></span></pre>



<h3 class="p1 wp-block-heading"><span style="color: #000080;"><b>Visibility Modifiers Inside Classes and Interfaces</b></span></h3>



<p>Visibility modifiers for members (functions, properties) declared inside a class:</p>
<!-- WP QUADS Content Ad Plugin v. 2.0.98.1 -->
<div class="quads-location quads-ad2" id="quads-ad2" style="float:none;margin:0px;">

</div>




<ul class="wp-block-list"><li><span style="color: #0000ff;"><strong><span class="has-inline-color">public</span></strong></span> : visible to any client who can see the declaring class.</li><li><span style="color: #0000ff;"><strong><span class="has-inline-color">Private</span></strong></span>: visible inside the class only.</li><li><span style="color: #0000ff;"><strong><span class="has-inline-color">internal</span></strong></span>: visible to any client inside the module that can see the declaring class.</li><li><span style="color: #0000ff;"><strong><span class="has-inline-color">Protected</span></strong></span>: visible inside the class and its subclasses.</li></ul>



<p class="p1"><span style="color: #000080;"><b>Note:</b></span> ;If you override a ;protected ;member in the derived class without specifying its visibility, its visibility will also be ;<span style="color: #008000;"><strong>protected</strong></span>.</p>



<p><span style="color: #000080;"><strong>Example</strong></span></p>



<pre class="wp-block-preformatted">package com.mytest

open class Test01() {

 <span style="color: #008000;"><strong>// private to Test01(Base class)</strong></span>
 private fun privateMethod() {
 println("private method call")
 }

 <strong><span style="color: #008000;">// public by default and visible everywhere</span></strong>
 fun publicMethod() {
 privateMethod()
 println("public method call")
 }

<strong><span style="color: #008000;"> // visible to Test01(Base class) and Test02(Derived class)
</span></strong> protected fun protectedMethod() {
 println("protected method call")
 }

 <strong><span style="color: #008000;"> // visible inside the same module</span></strong>
 internal fun internalMethod() {
 println("internal method call")
 }
}

fun main() {
<span style="color: #008000;"><strong>//creating Test01 instance</strong></span>
 val test01 = Test01()

<strong><span style="color: #008000;"> //publicMethod() and internalMethod() of Test01 class are visible
</span></strong> test01.publicMethod()
 test01.internalMethod()

<span style="color: #0000ff;"><strong> //compile error: protectedMethod() and privateMethod() of Test01 class are not visible
</strong>
<strong> //test01.protectedMethod() <span style="color: #008000;"> //accessible in subclasses only
</span></strong>
<strong> //test01.privateMethod() <span style="color: #008000;">//accessible only inside the class Test01
</span></strong></span>
}

<span style="color: #008000;"><strong>// Test02 inheriting class Test01</strong></span>
class Test02 : Test01() {
 fun newMethod() {
 <strong><span style="color: #008000;">// protectedMethod() ,publicMethod() ,internalMethod() of the Test01 class are visible
 // privateMethod() is not visible</span></strong>
 protectedMethod()

 publicMethod()

 internalMethod()

 <strong><span style="color: #008000;"> //privateMethod() // compile error</span></strong>

 }
}</pre>



<h3 class="p1 wp-block-heading"><span style="color: #000080;"><b>Changing Visibility of a Constructor</b></span></h3>



<p class="p1">By default, the visibility of a ;constructor is ;<span style="color: #008000;"><strong>public</strong></span>. However, you can change it. For that, you need to explicitly add ;<strong><span style="color: #0000ff;">constructor</span></strong> ;keyword.</p>



<p class="p1">The constructor is ;<span style="color: #008000;"><strong>public</strong></span> ;by default in the example below:</p>



<pre class="wp-block-preformatted">class Test(val a: Int) {
 <strong><span style="color: #008000;"> // code</span></strong>
}

<strong><span style="color: #0000ff;">OR</span> 
</strong>
<span style="color: #008000;"><strong>//by default the constructor is public</strong></span>
class Test constructor(val a: Int) {
 <strong><span style="color: #008000;"> // code</span></strong> 
}</pre>



<p class="p1">Here&#8217;s how you can change its visibility. Here the constructor is <span style="color: #008000;"><strong>private</strong></span>.</p>



<pre class="wp-block-preformatted">class Test <strong><span style="color: #0000ff;">private</span></strong> constructor(val a: Int) {
 <strong><span style="color: #008000;">// code</span></strong>
}</pre>



<p><span style="color: #000080;"><strong>Note:</strong></span> ;In Kotlin, local functions, variables and classes cannot have visibility modifiers.

