{"id":967,"date":"2016-02-29T07:39:57","date_gmt":"2016-02-29T07:39:57","guid":{"rendered":"http:\/\/abhiandroid.com\/java\/?page_id=967"},"modified":"2016-02-29T07:39:57","modified_gmt":"2016-02-29T07:39:57","slug":"access-modifiers","status":"publish","type":"page","link":"https:\/\/abhiandroid.com\/java\/access-modifiers","title":{"rendered":"Access Modifiers In Java With Examples"},"content":{"rendered":"<p>Java provides us a many number of access modifiers to set access levels for <a href=\"\/java\/class-objects\">class<\/a>, <a href=\"\/java\/variables-with-examples\">variables<\/a>, methods and <a href=\"\/java\/constructor\">constructor<\/a>. It means the access modifiers in java specify scope of a data member, method, constructor or a class. The four access modifiers in JAVA are private, default, protected and public.<\/p>\n<hr \/>\n<h4><strong>Types of Access Modifiers<\/strong><\/h4>\n<p>There are 4 types of java access modifiers:<\/p>\n<ul>\n<li><strong>private &#8211;<\/strong> accessible\u00a0within class only<\/li>\n<li><strong>default &#8211;<\/strong> accessible available to the package only<\/li>\n<li><strong>protected &#8211;<\/strong>\u00a0accessible within package and outside the package but through inheritance only<\/li>\n<li><strong>public &#8211;<\/strong>\u00a0accessible everywhere<\/li>\n<\/ul>\n<p>Below we discuss all four access modifiers in detail:<\/p>\n<hr \/>\n<h4><strong><span style=\"text-decoration: underline;\">Private Access Modifier:<\/span><\/strong><\/h4>\n<p>The private access modifier is accessible only within class. It means the methods, variables and constructors that are declared as private can only be accessed within the declared class itself. It is very restrictive access modifier.<\/p>\n<p>Variables that are declared private can be accessed outside the class if public getter methods are present in the class.<\/p>\n<p><span style=\"color: #ff0000;\"><strong>Important Note:<\/strong><\/span> We cannot make class and interfaces private.<\/p>\n<p><span style=\"color: #008000;\"><strong>Program example of private access modifier:<\/strong><\/span><\/p>\n<p>Let us take an example to show the use of private access modifier.<\/p>\n<p><span style=\"color: #008000;\"><strong>Step 1:<\/strong><\/span> First we create class <strong>PrivateClass\u00a0<\/strong>in which we declare one private data member and one private method:<\/p>\n<pre>class\u00a0PrivateClass\r\n\r\n{\r\n\r\n  private\u00a0int\u00a0x= 10;\r\n\r\n  private\u00a0void\u00a0show()\r\n\r\n  {\r\n\r\n    System.out.println(\"Private class\");\r\n\r\n  }\r\n\r\n}\r\n<\/pre>\n<p><span style=\"color: #008000;\"><strong>Step 2:<\/strong><\/span> Second we create a class <strong>PrivateAccess <\/strong>in which we call the <strong>PrivateClass <\/strong>class data member and method:<\/p>\n<pre>class PrivateAccess\r\n\r\n{\r\n\r\n  public\u00a0static\u00a0void\u00a0main(String\u00a0args[])\r\n\r\n  {\r\n\r\n    PrivateClass\u00a0obj=new\u00a0PrivateClass();\r\n\r\n    System.out.println(obj.x);\u00a0 \/\/Compile\u00a0Time\u00a0Error\r\n\r\n    obj.show();\u00a0 \/\/Compile\u00a0Time\u00a0Error\r\n\r\n  }\r\n\r\n}<\/pre>\n<p><span style=\"color: #008000;\"><strong>Explanation of Example:<\/strong><\/span><\/p>\n<p>In <strong>PrivateAccess<\/strong> class when we are trying to call the private data member and method of a <strong>PrivateClass<\/strong> class it gives us compile time error because private data members and methods have a access level to <strong>PrivateClass <\/strong>class only.<\/p>\n<hr \/>\n<h4><strong>Default Access Modifier:<\/strong><\/h4>\n<p>If we don&#8217;t use any modifier, it is treated as\u00a0default access modifier\u00a0by default. In other words we can say it id\u00a0default if no access modifier for a class, method, field, etc is explicitly declared. The default modifier is accessible only within package.<\/p>\n<p>A variable or method which is declared without any access modifier is available to any other class in the same package.<\/p>\n<p><span style=\"color: #008000;\"><strong>Program Example of default access modifier<\/strong><\/span><\/p>\n<p>Let us take an example to show the use of default access modifier.<\/p>\n<p><span style=\"color: #008000;\"><strong>Step 1: <\/strong><\/span>First we create a class <strong>DefaultClass <\/strong>under a package <strong>pack <\/strong>in which we declare default method:<\/p>\n<pre>package pack;\r\n\r\nclass DefaultClass\r\n\r\n{\r\n\r\n  void show()\r\n\r\n  {\r\n\r\n    System.out.println(\"Default class\");\r\n\r\n  }\r\n\r\n}<\/pre>\n<p><span style=\"color: #008000;\"><strong>Step 2: <\/strong><\/span>Second we create a class <strong>DefaultAccess <\/strong>under a package <strong>mypack <\/strong>and imports the above package <strong>pack:<\/strong><\/p>\n<pre>package mypack;\r\n\r\nimport pack.*;\r\n\r\nclass DefaultAccess\r\n\r\n{\r\n\r\n  public static void main(String args[])\r\n\r\n  {\r\n\r\n    DefaultClass obj = new DefaultClass(); \/\/Compile Time Error\r\n\r\n    obj.show(); \/\/Compile Time Error\r\n\r\n  }\r\n\r\n}<\/pre>\n<p><strong>Explanation of Example:<\/strong><\/p>\n<p>In the above example, the scope of class <strong>DefaultClass<\/strong> and its method <strong>show()<\/strong> is default so it cannot be accessed from outside the package. As a result it shows compile time error.<\/p>\n<hr \/>\n<h4><strong>Protected Access Modifier:<\/strong><\/h4>\n<p>We can access\u00a0protected access modifier either\u00a0within package and outside the package but through inheritance only. It means variables, methods and constructors which are declared as protected in a base class can be accessed only by the child classes in other package.<\/p>\n<p>The protected access modifier can be applied on the data member, method and constructor. It cannot be applied on the class and interfaces. Protected access modifier gives a chance to the child class to use the helper method or variable, while preventing from trying to use a class i.e. non related.<\/p>\n<p><span style=\"color: #008000;\"><strong>Program Example of protected access modifier<\/strong><\/span><\/p>\n<p>Let us take an example to show use of protected access modifier:<\/p>\n<p><span style=\"color: #008000;\"><strong>Step 1: <\/strong><\/span>First we create a class <strong>ProtectedClass <\/strong>under a package <strong>pack1 <\/strong>in which we declare a protected method show():<\/p>\n<pre>package pack1;\r\n\r\nclass ProtectedClass\r\n\r\n{\r\n\r\n  protected void show()\r\n\r\n  {\r\n\r\n    System.out.println(\"Protected class\");\r\n\r\n  }\r\n\r\n}<\/pre>\n<p><span style=\"color: #008000;\"><strong>Step 2: <\/strong><\/span>Second we create a class <strong>ProtectedAccess <\/strong>under a package <strong>mypack1 <\/strong>and import a package <strong>pack1:<\/strong><\/p>\n<pre>package mypack1;\r\n\r\nimport pack1.*;\r\n\r\nclass ProtectedAccess extends ProtectedClass\r\n\r\n{\r\n\r\n  public static void main(String args[])\r\n\r\n  {\r\n\r\n    ProtectedAccess obj = new ProtectedAccess();\r\n\r\n    obj.show();\r\n\r\n  }\r\n\r\n}<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<pre>Protected class<\/pre>\n<p><strong>Explanation of Example:<\/strong><\/p>\n<p>The ProtectedClass class of pack1 package is public, so it can be accessed from outside the package. But show() method of this package is declared as protected, so it can be accessed from outside the class only through inheritance.<\/p>\n<hr \/>\n<h4><strong>Public Access Modifier:<\/strong><\/h4>\n<p>The\u00a0public access modifier\u00a0is accessible everywhere. It means a class, method, constructor, interface etc declared as public can be accessed from any other class. It has the widest scope among all other modifiers.<\/p>\n<p><span style=\"color: #008000;\"><strong>Program Example of public access modifier<\/strong><\/span><\/p>\n<p>Let us take an example to show the use of public access modifier.<\/p>\n<p><span style=\"color: #008000;\"><strong>Step 1: <\/strong><\/span>First we create a class <strong>PublicClass <\/strong>in which we declare the public method <strong>show()<\/strong>:<\/p>\n<pre>class PublicClass\r\n\r\n{\r\n\r\n  public void show()\r\n\r\n  {\r\n\r\n    System.out.println(\"Public class\");\r\n\r\n  }\r\n\r\n}<\/pre>\n<p><span style=\"color: #008000;\"><strong>Step 2: <\/strong><\/span>Second we create a class <strong>PublicAccess <\/strong>in which we call the method of <strong>PublicClass<\/strong> class:<\/p>\n<pre>class PublicAccess\r\n\r\n{\r\n\r\n  public static void main(String args[])\r\n\r\n  {\r\n\r\n    PublicClass obj = new PublicClass();\r\n\r\n    obj.show();\r\n\r\n  }\r\n\r\n}<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<pre>Public class<\/pre>\n<hr \/>\n<h4><strong>Understanding all access modifiers in java<\/strong><\/h4>\n<p>Let&#8217;s understand the access modifiers with the help of following table:<\/p>\n<table border=\"1\">\n<tbody>\n<tr>\n<td>Access Modifier<\/td>\n<td>within a class<\/td>\n<td>within a package<\/td>\n<td>outside a package by subclass only<\/td>\n<td>outside a package<\/td>\n<\/tr>\n<tr>\n<td>Private<\/td>\n<td>Yes<\/td>\n<td>No<\/td>\n<td>No<\/td>\n<td>No<\/td>\n<\/tr>\n<tr>\n<td>Default<\/td>\n<td>Yes<\/td>\n<td>Yes<\/td>\n<td>No<\/td>\n<td>No<\/td>\n<\/tr>\n<tr>\n<td>Protected<\/td>\n<td>Yes<\/td>\n<td>Yes<\/td>\n<td>Yes<\/td>\n<td>No<\/td>\n<\/tr>\n<tr>\n<td>Public<\/td>\n<td>Yes<\/td>\n<td>Yes<\/td>\n<td>Yes<\/td>\n<td>Yes<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n","protected":false},"excerpt":{"rendered":"<p>Java provides us a many number of access modifiers to set access levels for class, variables, methods and constructor. It means the access modifiers in java specify scope of a data member, method, constructor or a class. The four access modifiers in JAVA are private, default, protected and public. Types of Access Modifiers There are &hellip; <a href=\"https:\/\/abhiandroid.com\/java\/access-modifiers\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Access Modifiers In Java With Examples<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"parent":0,"menu_order":0,"comment_status":"open","ping_status":"closed","template":"home.php","meta":{"footnotes":""},"class_list":["post-967","page","type-page","status-publish","hentry"],"psp_head":"<title>Access Modifiers In Java With Examples \u2013 Abhi Android<\/title>\r\n<meta name=\"description\" content=\"Learn 4 access modifiers (private, default, protected and public) in JAVA with example. Java provides us a many number of access modifiers to set access levels for classes, variables, methods and constructors.\" \/>\r\n<meta name=\"robots\" content=\"index,follow\" \/>\r\n<link rel=\"canonical\" href=\"https:\/\/abhiandroid.com\/java\/access-modifiers\" \/>\r\n","_links":{"self":[{"href":"https:\/\/abhiandroid.com\/java\/wp-json\/wp\/v2\/pages\/967","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/abhiandroid.com\/java\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/abhiandroid.com\/java\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/abhiandroid.com\/java\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/abhiandroid.com\/java\/wp-json\/wp\/v2\/comments?post=967"}],"version-history":[{"count":0,"href":"https:\/\/abhiandroid.com\/java\/wp-json\/wp\/v2\/pages\/967\/revisions"}],"wp:attachment":[{"href":"https:\/\/abhiandroid.com\/java\/wp-json\/wp\/v2\/media?parent=967"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}