<p>This post is about <strong>Access/Visibility modifiers in Java</strong> and how to use them. </p>



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



<p>In Java, access modifiers are used to set the accessibility (visibility) of <strong>classes, constructors, data members </strong>and<strong> methods</strong> in another classes. ;</p>



<p>There are four types of access modifiers available in java:</p>



<ol class="wp-block-list"><li><strong><span style="color:#0000ff" class="has-inline-color">Default</span></strong> : visible only within the package.</li><li><strong><span style="color:#0000ff" class="has-inline-color">Private</span></strong>: visible within the class only.</li><li><strong><span style="color:#0000ff" class="has-inline-color">Protected</span></strong>: visible within the package or all subclasses (any package).</li><li><strong><span style="color:#0000ff" class="has-inline-color">Public</span></strong>: visible everywhere in entire project.</li></ol>



<p>Let&#8217;s understand each modifier in detail with simple example.</p>



<h3 class="wp-block-heading"><strong><span style="color:#520599" class="has-inline-color">Default Access Modifier</span></strong></h3>



<p>When no access modifier is specified, its the ;<strong><span style="color:#04603e" class="has-inline-color">default</span></strong> ;access modifier by default.</p>



<p>The data members, class or methods which are not declared using any access modifiers i.e. having default access modifier are accessible ;<span style="color:#04603e" class="has-inline-color"><strong>only within the same package</strong>.</span></p>



<h5 class="wp-block-heading"><strong><span style="color:#560399" class="has-inline-color">Example</span></strong></h5>



<p>In this example we have two classes, TestC class is trying to access the default members of TestA class, since class TestC belongs to a different package, this program would throw compilation error, because the scope of default modifier is limited to the same package in which it is declared.</p>



<p><strong><span style="color:#0000ff" class="has-inline-color">TestA.java</span></strong></p>



<pre class="wp-block-preformatted"><strong>package <span style="color:#560399" class="has-inline-color">com.c1ctech.pkg1</span></strong>;

<em><span style="color:#04603e" class="has-inline-color"><strong>//public class</strong></span>
</em><strong>public class </strong>TestA {

 <em><strong><span style="color:#04603e" class="has-inline-color">//default data member</span></strong>
 </em><strong>int a</strong>;

 <em><strong><span style="color:#04603e" class="has-inline-color">//default method</span></strong>
 </em><strong>void </strong>defaultMethod() {
 System.<strong><em>out</em></strong>.println(<strong>"defaultMethod of class TestA"</strong>);
 }
}</pre>



<p><strong><span style="color:#0000ff" class="has-inline-color">TestC.java</span></strong></p>



<pre class="wp-block-preformatted"><strong>package <span style="color:#560399" class="has-inline-color">com.c1ctech.pkg2</span></strong>;

<strong>import </strong>com.c1ctech.pkg1.TestA;

<em><strong><span style="color:#04603e" class="has-inline-color">//default class</span></strong>
</em><strong>class </strong>TestC {

 <strong>public static void </strong>main(String[] args) {

 <em><strong><span style="color:#04603e" class="has-inline-color">//error: cannot access default members outside the package</span></strong>
 </em><strong>new </strong>TestA().<strong>a </strong>= 10;
 <strong>new </strong>TestA().defaultMethod();

 }
}</pre>



<h3 class="wp-block-heading"><strong><span style="color:#520599" class="has-inline-color">Private Access Modifier</span></strong></h3>



<p>The scope of private modifier is limited to the class only.</p>



<ul class="wp-block-list"><li>Private Data members and methods are only accessible within the class.</li></ul>



<ul class="wp-block-list"><li><strong><span style="color:#04603e" class="has-inline-color">Class</span></strong> and ;<strong><span style="color:#04603e" class="has-inline-color">Interface</span></strong> cannot be declared as <strong><span style="color:#04603e" class="has-inline-color">private</span></strong>.</li></ul>



<h5 class="wp-block-heading"><strong><span style="color:#520599" class="has-inline-color">Example</span></strong></h5>



<p>This example throws compilation error because we are trying to access the private data member and method of class TestA in class TestB. The private data member and method are only accessible within the class.</p>



<p><strong><span style="color:#0000ff" class="has-inline-color">TestA.java</span></strong></p>



<pre class="wp-block-preformatted"><strong>package </strong>com.c1ctech.pkg1;

<em><strong><span style="color:#04603e" class="has-inline-color">//public class</span></strong>
</em><strong>public class </strong>TestA {

 <em><strong><span style="color:#04603e" class="has-inline-color">//private data member</span></strong>
 </em><strong>private int a</strong>;

 <em><strong><span style="color:#04603e" class="has-inline-color">//private method</span></strong>
 </em><strong>private void </strong>privateMethod() {
 System.<strong><em>out</em></strong>.println(<strong>"privateMethod of class TestA"</strong>);
 }

 <strong>void </strong>defaultMethod() {
 System.<strong><em>out</em></strong>.println(<strong>"defaultMethod of class TestA"</strong>);
 <em><strong><span style="color:#04603e" class="has-inline-color">//access private method within class</span></strong>
 </em>privateMethod();
 }
}</pre>



<p><strong><span style="color:#0000ff" class="has-inline-color">TestB.java</span></strong></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>




<pre class="wp-block-preformatted"><strong>package </strong>com.c1ctech.pkg1;

<strong>class </strong>TestB {

 <strong>public static void </strong>main(String[] args) {

 <em><strong><span style="color:#04603e" class="has-inline-color">//error: cannot access private members outside the class TestA</span></strong>
 </em><strong>new </strong>TestA().<strong>a </strong>= 10;
 <strong>new </strong>TestA().privateMethod();
 }

}</pre>



<h3 class="wp-block-heading"><strong><span style="color:#520599" class="has-inline-color">Protected Access Modifier</span></strong></h3>



<p>Protected data member and method are only accessible by the classes of the same package and the subclasses present in any package. </p>



<p>Protected access modifier is similar to default access modifier with one exception that it has visibility in sub classes.</p>



<p>Classes cannot be declared as <strong><span style="color:#04603e" class="has-inline-color">protected</span></strong>. ;</p>



<h5 class="wp-block-heading"><strong><span style="color:#560399" class="has-inline-color">Example</span></strong></h5>



<p>In this example the class <strong>TestC</strong> which is present in another package is able to access the ;protected members of class <strong>TestA</strong>. This is because the TestC class extends class TestA and the protected modifier allows the access of protected members in subclasses (in any packages).</p>



<p><strong><span style="color:#0000ff" class="has-inline-color">TestA.java</span></strong></p>



<pre class="wp-block-preformatted"><strong>package <span style="color:#520599" class="has-inline-color">com.c1ctech.pkg1</span></strong>;

<em><strong><span style="color:#04603e" class="has-inline-color">//public class</span></strong>
</em><strong>public class </strong>TestA {

 <em><strong><span style="color:#04603e" class="has-inline-color">//protected data member</span></strong>
 </em><strong>protected int a</strong>;

 <em><strong><span style="color:#04603e" class="has-inline-color">//protected method</span></strong>
 </em><strong>protected void </strong>protectedMethod() {
 System.<strong><em>out</em></strong>.println(<strong>"protectedMethod of class TestA"</strong>);
 }<em>
</em>}</pre>



<p><strong><span style="color:#0000ff" class="has-inline-color">TestC.java</span></strong></p>



<pre class="wp-block-preformatted"><strong>package <span style="color:#520599" class="has-inline-color">com.c1ctech.pkg2</span></strong>;

<strong>import </strong>com.c1ctech.pkg1.TestA;

<em><strong><span style="color:#04603e" class="has-inline-color">//default class</span></strong>
</em><strong>class </strong>TestC <strong>extends </strong>TestA {

 <strong>public static void </strong>main(String[] args) {

 <em><strong><span style="color:#04603e" class="has-inline-color">// compile time error</span></strong>
 </em><strong>//new </strong>TestA().<strong>a </strong>= 10;
 //<strong>new </strong>TestA().protectedMethod();

 <em><strong><span style="color:#04603e" class="has-inline-color">// works, accessing super class protected members using subclass</span></strong>
 </em><strong>new </strong>TestC().<strong>a </strong>= 10;
 <strong>new </strong>TestC().protectedMethod();
 }
}</pre>



<h3 class="wp-block-heading"><strong><span style="color:#520599" class="has-inline-color">Public Access Modifier</span></strong></h3>



<p>The data members, methods and classes that are declared <strong><span style="color:#04603e" class="has-inline-color">public</span></strong> can be accessed from anywhere. This modifier doesn’t put any restriction on the access.</p>



<h5 class="wp-block-heading"><strong><span style="color:#560399" class="has-inline-color">Example</span></strong></h5>



<p>In this example we have two classes, TestC class is trying to access the public members of TestA class (belongs to different package ) and it works as public members are accessible everywhere.</p>



<p><strong><span style="color:#0000ff" class="has-inline-color">TestA.java</span></strong></p>



<pre class="wp-block-preformatted"><strong>package </strong>com.c1ctech.pkg1;

<em><strong><span style="color:#04603e" class="has-inline-color">//public class</span></strong>
</em><strong>public class </strong>TestA {

 <em><strong><span style="color:#04603e" class="has-inline-color">//public data member</span></strong>
 </em><strong>public int a</strong>;

 <em><strong><span style="color:#04603e" class="has-inline-color">//public method</span></strong>
 </em><strong>public void </strong>publicMethod() {
 System.<strong><em>out</em></strong>.println(<strong>"publicMethod of class TestA"</strong>);
 }
}</pre>



<p><strong><span style="color:#0000ff" class="has-inline-color">TestC.java</span></strong></p>



<pre class="wp-block-preformatted"><strong>package </strong>com.c1ctech.pkg2;

<strong>import </strong>com.c1ctech.pkg1.TestA;

<em><strong><span style="color:#04603e" class="has-inline-color">//default class</span></strong>
</em><strong>class </strong>TestC {

 <strong>public static void </strong>main(String[] args) {

 <em><strong><span style="color:#04603e" class="has-inline-color">//works, public members are accessible everywhere.</span></strong>
 </em><strong>new </strong>TestA().<strong>a </strong>= 10;
 <strong>new </strong>TestA().publicMethod();<em>
 </em>}
}</pre>


