<div dir="ltr" style="text-align: justify;" trbidi="on">
<div dir="ltr" style="text-align: justify;" trbidi="on">Access modifiers specifies who can access them. There are four access modifiers used in java. They are public, private, protected, no modifier (declaring without an access modifier). Using ‘no modifier’ is also sometimes referred as ‘package-private’ or ‘default’ or ‘friendly’ access. Usage of these access modifiers is restricted to two levels. The two levels are class level access modifiers and ;member level access modifiers.<br />
<b><br />
I) Class level access modifiers (java classes only)</b><br />
Only two access modifiers is allowed, <i>public and no modifier</i></p>
<ul>
<li>If a class is ‘public’, then it CAN be accessed from ANYWHERE.</li>
<li>If a class has ‘no <b>modifier</b>’, then it CAN ONLY be accessed from ‘same package’.</li>
</ul>
<div align='center' id="ads-id"></div>
<p><b><br />
II) Member level access modifiers (java variables and java methods)</b><br />
All the four <i>public, private, protected and no modifier</i> is allowed.</p>
<ul>
<li>public and no modifier – the same way as used in class level.</li>
<li>private – members CAN ONLY access.</li>
<li>protected – CAN be accessed from ‘same package’ and a subclass existing in any package can access.</li>
</ul>
<p>For better understanding, member level access is formulated as a table:</p>
<table border="1" cellspacing="0">
<tbody>
<tr>
<td><i><br />
</i> <b><br />
<i>Access Modifiers</i></b><br />
<i> </i></td>
<td><i>Same Class</i></td>
<td><i>Same Package</i></td>
<td><i>Subclass</i></td>
<td><i>Other packages</i></td>
</tr>
<tr>
<td><b>public</b></td>
<td style="text-align: center;">Y</td>
<td style="text-align: center;">Y</td>
<td style="text-align: center;">Y</td>
<td style="text-align: center;">Y</td>
</tr>
<tr>
<td style="text-align: left;"><b>protected</b></td>
<td style="text-align: center;">Y</td>
<td style="text-align: center;">Y</td>
<td style="text-align: center;">Y</td>
<td style="text-align: center;">N</td>
</tr>
<tr>
<td style="text-align: left;"><b>no access modifier</b></td>
<td style="text-align: center;">Y</td>
<td style="text-align: center;">Y</td>
<td style="text-align: center;">N</td>
<td style="text-align: center;">N</td>
</tr>
<tr>
<td style="text-align: left;"><b>private</b></td>
<td style="text-align: center;">Y</td>
<td style="text-align: center;">N</td>
<td style="text-align: center;">N</td>
<td style="text-align: center;">N</td>
</tr>
</tbody>
</table>
<p>First row {public Y Y Y Y} should be interpreted as:</p>
<ul>
<li>Y – A member declared with ‘public’ access modifier CAN be accessed by the members of the ‘same class’.</li>
<li>Y – A member declared with ‘public’ access modifier CAN be accessed by the members of the ‘same package’.</li>
<li>Y – A member declared with ‘public’ access modifier CAN be accessed by the members of the ‘subclass’.</li>
<li>Y – A member declared as ‘public’ CAN be accessed from ‘Other packages’.</li>
</ul>
<p>Second row {protected Y Y Y N} should be interpreted as:</p>
<ul>
<li>Y – A member declared with ‘protected’ access modifier CAN be accessed by the members of the ‘same class’.</li>
<li>Y – A member declared with ‘protected’ access modifier CAN be accessed by the members of the ‘same package’.</li>
<li>Y – A member declared with ‘protected’ access modifier CAN be accessed by the members of the ‘subclass’.</li>
<li>N – A member declared with ‘protected’ access modifier CANNOT be accessed by the members of the ‘Other package’.</li>
</ul>
<p>similarly interpret the access modifiers table for the third (no access modifier) and fourth (private access modifier) records.</p></div>
<p></p>
<div style="background-color: pink; border-width: thin; text-align: center;"><b><;<;<a href="https://dineshonjava.com/2013/02/objects-and-classes-in-java.html">Previous</a> <;<; ; ; || <a href="https://dineshonjava.com/2013/01/core-java-baby-step-to-be-best-java-ian.html">Index </a>||  ; >;>;<a href="https://dineshonjava.com/2013/03/methods-in-java.html">Next</a> >;>;</b></div>
</div>
<div class="wp-post-navigation"> 
									 <div class="wp-post-navigation-pre"> 
									 <a href="https://dineshonjava.com/objects-and-classes-in-java/">Previous</a> 
									 </div> 
									 <div class="wp-post-navigation-next"> 
									 <a href="https://dineshonjava.com/java-string-charat-method/">Next</a> 
									 </div> 
									</div>
<script type="text/javascript">
jQuery(document).ready(function($) {
 $.post('https://dineshonjava.com/wp-admin/admin-ajax.php', {action: 'mts_view_count', id: '555'});
});
</script>
Strategy Design Patterns We can easily create a strategy design pattern using lambda. To implement…
Decorator Pattern A decorator pattern allows a user to add new functionality to an existing…
Delegating pattern In software engineering, the delegation pattern is an object-oriented design pattern that allows…
Technology has emerged a lot in the last decade, and now we have artificial intelligence;…
Managing a database is becoming increasingly complex now due to the vast amount of data…
Overview In this article, we will explore Spring Scheduler how we could use it by…