{"id":25545513,"date":"2022-10-23T13:29:15","date_gmt":"2022-10-23T07:59:15","guid":{"rendered":"https:\/\/entri.app\/blog\/?p=25545513"},"modified":"2022-11-23T14:18:34","modified_gmt":"2022-11-23T08:48:34","slug":"what-is-access-modifiers-in-java-a-short-note","status":"publish","type":"post","link":"https:\/\/entri.app\/blog\/what-is-access-modifiers-in-java-a-short-note\/","title":{"rendered":"What is Access Modifiers in Java- A Short Note"},"content":{"rendered":"<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_79_2 counter-hierarchy ez-toc-counter ez-toc-custom ez-toc-container-direction\">\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">Table of Contents<\/p>\n<label for=\"ez-toc-cssicon-toggle-item-69e69eb61caea\" class=\"ez-toc-cssicon-toggle-label\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #999;color:#999\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewBox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #999;color:#999\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewBox=\"0 0 24 24\" version=\"1.2\" baseProfile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/label><input type=\"checkbox\"  id=\"ez-toc-cssicon-toggle-item-69e69eb61caea\"  aria-label=\"Toggle\" \/><nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/entri.app\/blog\/what-is-access-modifiers-in-java-a-short-note\/#Protected_Access_Modifier_%E2%80%93_Protected\" >Protected Access Modifier &#8211; Protected<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/entri.app\/blog\/what-is-access-modifiers-in-java-a-short-note\/#Example\" >Example<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/entri.app\/blog\/what-is-access-modifiers-in-java-a-short-note\/#Introduction_to_Private_in_Java\" >Introduction to Private in Java<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/entri.app\/blog\/what-is-access-modifiers-in-java-a-short-note\/#Rules_and_Regulations_for_Private_Constructor_in_Java\" >Rules and Regulations for\u00a0Private Constructor in Java<\/a><\/li><\/ul><\/nav><\/div>\n<h1 class=\"h1\"><\/h1>\n<p>There are two types of modifiers in Java:\u00a0<strong>access modifiers<\/strong>\u00a0and\u00a0<strong>non-access modifiers<\/strong>.<\/p>\n<p>The access modifiers in Java specifies the accessibility or scope of a field, method, constructor, or class. We can change the access level of fields, constructors, methods, and class by applying the access modifier on it.<\/p>\n<p>There are four types of Java access modifiers:<\/p>\n<ol>\n<li><strong>Private<\/strong>: The access level of a private modifier is only within the class. It cannot be accessed from outside the class.<\/li>\n<li><strong>Default<\/strong>: The access level of a default modifier is only within the package. It cannot be accessed from outside the package. If you do not specify any access level, it will be the default.<\/li>\n<li><strong>Protected<\/strong>: The access level of a protected modifier is within the package and outside the package through child class. If you do not make the child class, it cannot be accessed from outside the package.<\/li>\n<li><strong>Public<\/strong>: The access level of a public modifier is everywhere. It can be accessed from within the class, outside the class, within the package and outside the package.<\/li>\n<\/ol>\n<p>There are many non-access modifiers, such as static, abstract, synchronized, native, volatile, transient, etc. Here, we are going to learn the access modifiers only.<\/p>\n<p><a href=\"https:\/\/bit.ly\/3ELmCiA\" target=\"_blank\" rel=\"noopener\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-25522670 size-full\" src=\"https:\/\/entri.app\/blog\/wp-content\/uploads\/2022\/04\/Python-and-Machine-Learning-Rectangle-1.png\" alt=\"Python and Machine Learning Rectangle\" width=\"970\" height=\"250\" srcset=\"https:\/\/entri.app\/blog\/wp-content\/uploads\/2022\/04\/Python-and-Machine-Learning-Rectangle-1.png 970w, https:\/\/entri.app\/blog\/wp-content\/uploads\/2022\/04\/Python-and-Machine-Learning-Rectangle-1-300x77.png 300w, https:\/\/entri.app\/blog\/wp-content\/uploads\/2022\/04\/Python-and-Machine-Learning-Rectangle-1-768x198.png 768w, https:\/\/entri.app\/blog\/wp-content\/uploads\/2022\/04\/Python-and-Machine-Learning-Rectangle-1-750x193.png 750w\" sizes=\"auto, (max-width: 970px) 100vw, 970px\" \/><\/a><\/p>\n<p><strong style=\"text-align: center; color: #1d1f20; font-size: 1.953em;\">Understanding Java Access Modifiers<\/strong><\/p>\n<p>Let&#8217;s understand the access modifiers in Java by a simple table.<\/p>\n<table class=\"alt\">\n<tbody>\n<tr>\n<th>Access Modifier<\/th>\n<th>within class<\/th>\n<th>within package<\/th>\n<th>outside package by subclass only<\/th>\n<th>outside package<\/th>\n<\/tr>\n<tr>\n<td><b>Private<\/b><\/td>\n<td>Y<\/td>\n<td>N<\/td>\n<td>N<\/td>\n<td>N<\/td>\n<\/tr>\n<tr>\n<td><b>Default<\/b><\/td>\n<td>Y<\/td>\n<td>Y<\/td>\n<td>N<\/td>\n<td>N<\/td>\n<\/tr>\n<tr>\n<td><b>Protected<\/b><\/td>\n<td>Y<\/td>\n<td>Y<\/td>\n<td>Y<\/td>\n<td>N<\/td>\n<\/tr>\n<tr>\n<td><b>Public<\/b><\/td>\n<td>Y<\/td>\n<td>Y<\/td>\n<td>Y<\/td>\n<td>Y<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h3 class=\"h2\">1) Private<\/h3>\n<p>The private access modifier is accessible only within the class.<\/p>\n<p><strong>Simple example of private access modifier<\/strong><\/p>\n<p>In this example, we have created two classes A and Simple. A class contains private data member and private method. We are accessing these private members from outside the class, so there is a compile-time error.<\/p>\n<div class=\"codeblock\">\n<div class=\"dp-highlighter\">\n<ol class=\"dp-j\" start=\"1\">\n<li class=\"alt\"><span class=\"keyword\">class<\/span>\u00a0A{<\/li>\n<li class=\"\"><span class=\"keyword\">private<\/span>\u00a0<span class=\"keyword\">int<\/span>\u00a0data=<span class=\"number\">40<\/span>;<\/li>\n<li class=\"alt\"><span class=\"keyword\">private<\/span>\u00a0<span class=\"keyword\">void<\/span>\u00a0msg(){System.out.println(<span class=\"string\">&#8220;Hello\u00a0java&#8221;<\/span>);}<\/li>\n<li class=\"\">}<\/li>\n<li class=\"alt\"><\/li>\n<li class=\"\"><span class=\"keyword\">public<\/span>\u00a0<span class=\"keyword\">class<\/span>\u00a0Simple{<\/li>\n<li class=\"alt\">\u00a0<span class=\"keyword\">public<\/span>\u00a0<span class=\"keyword\">static<\/span>\u00a0<span class=\"keyword\">void<\/span>\u00a0main(String\u00a0args[]){<\/li>\n<li class=\"\">\u00a0\u00a0\u00a0A\u00a0obj=<span class=\"keyword\">new<\/span>\u00a0A();<\/li>\n<li class=\"alt\">\u00a0\u00a0\u00a0System.out.println(obj.data);<span class=\"comment\">\/\/Compile\u00a0Time\u00a0Error<\/span><\/li>\n<li class=\"\">\u00a0\u00a0\u00a0obj.msg();<span class=\"comment\">\/\/Compile\u00a0Time\u00a0Error<\/span><\/li>\n<li class=\"alt\">\u00a0\u00a0\u00a0}<\/li>\n<li class=\"\">}<\/li>\n<\/ol>\n<\/div>\n<\/div>\n<div class=\"adp_interactive_ad\"><\/div>\n<h3 class=\"h4\">Role of Private Constructor<\/h3>\n<p>If you make any class constructor private, you cannot create the instance of that class from outside the class. For example:<\/p>\n<div class=\"codeblock\">\n<div class=\"dp-highlighter\">\n<div class=\"bar\">\n<div class=\"tools\">\n<div class=\"adp_interactive_ad\"><\/div>\n<\/div>\n<\/div>\n<ol class=\"dp-j\" start=\"1\">\n<li class=\"alt\"><span class=\"keyword\">class<\/span>\u00a0A{<\/li>\n<li class=\"\"><span class=\"keyword\">private<\/span>\u00a0A(){}<span class=\"comment\">\/\/private\u00a0constructor<\/span><\/li>\n<li class=\"alt\"><span class=\"keyword\">void<\/span>\u00a0msg(){System.out.println(<span class=\"string\">&#8220;Hello\u00a0java&#8221;<\/span>);}<\/li>\n<li class=\"\">}<\/li>\n<li class=\"alt\"><span class=\"keyword\">public<\/span>\u00a0<span class=\"keyword\">class<\/span>\u00a0Simple{<\/li>\n<li class=\"\">\u00a0<span class=\"keyword\">public<\/span>\u00a0<span class=\"keyword\">static<\/span>\u00a0<span class=\"keyword\">void<\/span>\u00a0main(String\u00a0args[]){<\/li>\n<li class=\"alt\">\u00a0\u00a0\u00a0A\u00a0obj=<span class=\"keyword\">new<\/span>\u00a0A();<span class=\"comment\">\/\/Compile\u00a0Time\u00a0Error<\/span><\/li>\n<li class=\"\">\u00a0}<\/li>\n<li class=\"alt\">}<\/li>\n<\/ol>\n<\/div>\n<\/div>\n<h4 class=\"n\">Note: A class cannot be private or protected except nested class.<\/h4>\n<p style=\"text-align: center;\"><strong><a href=\"https:\/\/bit.ly\/3ELmCiA\" target=\"_blank\" rel=\"noopener\">Grab the opportunity to learn Python with Entri! Click Here<\/a><\/strong><\/p>\n<h3 class=\"h2\">2) Default<\/h3>\n<p>If you don&#8217;t use any modifier, it is treated as\u00a0<strong>default<\/strong>\u00a0by default. The default modifier is accessible only within package. It cannot be accessed from outside the package. It provides more accessibility than private. But, it is more restrictive than protected, and public.<\/p>\n<p><strong>Example of default access modifier<\/strong><\/p>\n<p>In this example, we have created two packages pack and mypack. We are accessing the A class from outside its package, since A class is not public, so it cannot be accessed from outside the package.<\/p>\n<div class=\"codeblock\">\n<div class=\"dp-highlighter\">\n<div class=\"bar\">\n<div class=\"tools\">\n<div class=\"adp_interactive_ad\"><\/div>\n<\/div>\n<\/div>\n<ol class=\"dp-j\" start=\"1\">\n<li class=\"alt\"><span class=\"comment\">\/\/save\u00a0by\u00a0A.java<\/span><\/li>\n<li class=\"\"><span class=\"keyword\">package<\/span>\u00a0pack;<\/li>\n<li class=\"alt\"><span class=\"keyword\">class<\/span>\u00a0A{<\/li>\n<li class=\"\">\u00a0\u00a0<span class=\"keyword\">void<\/span>\u00a0msg(){System.out.println(<span class=\"string\">&#8220;Hello&#8221;<\/span>);}<\/li>\n<li class=\"alt\">}<\/li>\n<\/ol>\n<\/div>\n<\/div>\n<div class=\"codeblock\">\n<div class=\"dp-highlighter\">\n<div class=\"bar\"><\/div>\n<ol class=\"dp-j\" start=\"1\">\n<li class=\"alt\"><span class=\"comment\">\/\/save\u00a0by\u00a0B.java<\/span><\/li>\n<li class=\"\"><span class=\"keyword\">package<\/span>\u00a0mypack;<\/li>\n<li class=\"alt\"><span class=\"keyword\">import<\/span>\u00a0pack.*;<\/li>\n<li class=\"\"><span class=\"keyword\">class<\/span>\u00a0B{<\/li>\n<li class=\"alt\">\u00a0\u00a0<span class=\"keyword\">public<\/span>\u00a0<span class=\"keyword\">static<\/span>\u00a0<span class=\"keyword\">void<\/span>\u00a0main(String\u00a0args[]){<\/li>\n<li class=\"\">\u00a0\u00a0\u00a0A\u00a0obj\u00a0=\u00a0<span class=\"keyword\">new<\/span>\u00a0A();<span class=\"comment\">\/\/Compile\u00a0Time\u00a0Error<\/span><\/li>\n<li class=\"alt\">\u00a0\u00a0\u00a0obj.msg();<span class=\"comment\">\/\/Compile\u00a0Time\u00a0Error<\/span><\/li>\n<li class=\"\">\u00a0\u00a0}<\/li>\n<li class=\"alt\">}<\/li>\n<\/ol>\n<\/div>\n<\/div>\n<p>In the above example, the scope of class A and its method msg() is default so it cannot be accessed from outside the package.<\/p>\n<h3 class=\"h2\">3) Protected<\/h3>\n<p>The\u00a0<b>protected access modifier<\/b>\u00a0is accessible within package and outside the package but through inheritance only.<\/p>\n<p>The protected access modifier can be applied on the data member, method and constructor. It can&#8217;t be applied on the class.<\/p>\n<p>It provides more accessibility than the default modifer.<\/p>\n<p><strong>Example of protected access modifier<\/strong><\/p>\n<p>In this example, we have created the two packages pack and mypack. The A class of pack package is public, so can be accessed from outside the package. But msg method of this package is declared as protected, so it can be accessed from outside the class only through inheritance.<\/p>\n<div class=\"codeblock\">\n<div class=\"dp-highlighter\">\n<div class=\"bar\">\n<div class=\"tools\">\n<div class=\"adp_interactive_ad\"><\/div>\n<\/div>\n<\/div>\n<ol class=\"dp-j\" start=\"1\">\n<li class=\"alt\"><span class=\"comment\">\/\/save\u00a0by\u00a0A.java<\/span><\/li>\n<li class=\"\"><span class=\"keyword\">package<\/span>\u00a0pack;<\/li>\n<li class=\"alt\"><span class=\"keyword\">public<\/span>\u00a0<span class=\"keyword\">class<\/span>\u00a0A{<\/li>\n<li class=\"\"><span class=\"keyword\">protected<\/span>\u00a0<span class=\"keyword\">void<\/span>\u00a0msg(){System.out.println(<span class=\"string\">&#8220;Hello&#8221;<\/span>);}<\/li>\n<li class=\"alt\">}<\/li>\n<\/ol>\n<\/div>\n<\/div>\n<div class=\"codeblock\">\n<div class=\"dp-highlighter\">\n<div class=\"bar\">\n<div class=\"tools\">\n<div class=\"adp_interactive_ad\"><\/div>\n<div class=\"adp_interactive_ad\"><\/div>\n<div class=\"adp_interactive_ad\"><\/div>\n<\/div>\n<\/div>\n<ol class=\"dp-j\" start=\"1\">\n<li class=\"alt\"><span class=\"comment\">\/\/save\u00a0by\u00a0B.java<\/span><\/li>\n<li class=\"\"><span class=\"keyword\">package<\/span>\u00a0mypack;<\/li>\n<li class=\"alt\"><span class=\"keyword\">import<\/span>\u00a0pack.*;<\/li>\n<li class=\"\"><\/li>\n<li class=\"alt\"><span class=\"keyword\">class<\/span>\u00a0B\u00a0<span class=\"keyword\">extends<\/span>\u00a0A{<\/li>\n<li class=\"\">\u00a0\u00a0<span class=\"keyword\">public<\/span>\u00a0<span class=\"keyword\">static<\/span>\u00a0<span class=\"keyword\">void<\/span>\u00a0main(String\u00a0args[]){<\/li>\n<li class=\"alt\">\u00a0\u00a0\u00a0B\u00a0obj\u00a0=\u00a0<span class=\"keyword\">new<\/span>\u00a0B();<\/li>\n<li class=\"\">\u00a0\u00a0\u00a0obj.msg();<\/li>\n<li class=\"alt\">\u00a0\u00a0}<\/li>\n<li class=\"\">}<\/li>\n<\/ol>\n<\/div>\n<\/div>\n<div class=\"codeblock3\">\n<pre>Output:Hello\r\n<\/pre>\n<\/div>\n<h3 class=\"h2\">4) Public<\/h3>\n<p>The\u00a0<strong>public access modifier<\/strong>\u00a0is accessible everywhere. It has the widest scope among all other modifiers.<\/p>\n<p><strong>Example of public access modifier<\/strong><\/p>\n<div class=\"codeblock\">\n<div class=\"dp-highlighter\">\n<div class=\"bar\">\n<div class=\"tools\">\n<div class=\"adp_interactive_ad\"><\/div>\n<div class=\"adp_interactive_ad\"><\/div>\n<\/div>\n<\/div>\n<ol class=\"dp-j\" start=\"1\">\n<li class=\"alt\"><span class=\"comment\">\/\/save\u00a0by\u00a0A.java<\/span><\/li>\n<li class=\"\"><\/li>\n<li class=\"alt\"><span class=\"keyword\">package<\/span>\u00a0pack;<\/li>\n<li class=\"\"><span class=\"keyword\">public<\/span>\u00a0<span class=\"keyword\">class<\/span>\u00a0A{<\/li>\n<li class=\"alt\"><span class=\"keyword\">public<\/span>\u00a0<span class=\"keyword\">void<\/span>\u00a0msg(){System.out.println(<span class=\"string\">&#8220;Hello&#8221;<\/span>);}<\/li>\n<li class=\"\">}<\/li>\n<\/ol>\n<\/div>\n<\/div>\n<div class=\"codeblock\">\n<div class=\"dp-highlighter\">\n<div class=\"bar\">\n<div class=\"tools\">\n<div class=\"adp_interactive_ad\"><\/div>\n<div class=\"adp_interactive_ad\"><\/div>\n<div class=\"adp_interactive_ad\"><\/div>\n<\/div>\n<\/div>\n<ol class=\"dp-j\" start=\"1\">\n<li class=\"alt\"><span class=\"comment\">\/\/save\u00a0by\u00a0B.java<\/span><\/li>\n<li class=\"\"><\/li>\n<li class=\"alt\"><span class=\"keyword\">package<\/span>\u00a0mypack;<\/li>\n<li class=\"\"><span class=\"keyword\">import<\/span>\u00a0pack.*;<\/li>\n<li class=\"alt\"><\/li>\n<li class=\"\"><span class=\"keyword\">class<\/span>\u00a0B{<\/li>\n<li class=\"alt\">\u00a0\u00a0<span class=\"keyword\">public<\/span>\u00a0<span class=\"keyword\">static<\/span>\u00a0<span class=\"keyword\">void<\/span>\u00a0main(String\u00a0args[]){<\/li>\n<li class=\"\">\u00a0\u00a0\u00a0A\u00a0obj\u00a0=\u00a0<span class=\"keyword\">new<\/span>\u00a0A();<\/li>\n<li class=\"alt\">\u00a0\u00a0\u00a0obj.msg();<\/li>\n<li class=\"\">\u00a0\u00a0}<\/li>\n<li class=\"alt\">}<\/li>\n<\/ol>\n<\/div>\n<\/div>\n<div class=\"codeblock3\">\n<pre>Output:Hello\r\n<\/pre>\n<\/div>\n<p style=\"text-align: center;\"><strong><a href=\"https:\/\/bit.ly\/3ELmCiA\" target=\"_blank\" rel=\"noopener\">Learn Coding in your Language! Enroll Here!<\/a><\/strong><\/p>\n<h3 class=\"h2\">Java Access Modifiers with Method Overriding<\/h3>\n<p>If you are overriding any method, overridden method (i.e. declared in subclass) must not be more restrictive.<\/p>\n<div class=\"codeblock\">\n<div class=\"dp-highlighter\">\n<ol class=\"dp-j\" start=\"1\">\n<li class=\"alt\"><span class=\"keyword\">class<\/span>\u00a0A{<\/li>\n<li class=\"\"><span class=\"keyword\">protected<\/span>\u00a0<span class=\"keyword\">void<\/span>\u00a0msg(){System.out.println(<span class=\"string\">&#8220;Hello\u00a0java&#8221;<\/span>);}<\/li>\n<li class=\"alt\">}<\/li>\n<li class=\"\"><\/li>\n<li class=\"alt\"><span class=\"keyword\">public<\/span>\u00a0<span class=\"keyword\">class<\/span>\u00a0Simple\u00a0<span class=\"keyword\">extends<\/span>\u00a0A{<\/li>\n<li class=\"\"><span class=\"keyword\">void<\/span>\u00a0msg(){System.out.println(<span class=\"string\">&#8220;Hello\u00a0java&#8221;<\/span>);}<span class=\"comment\">\/\/C.T.Error<\/span><\/li>\n<li class=\"alt\">\u00a0<span class=\"keyword\">public<\/span>\u00a0<span class=\"keyword\">static<\/span>\u00a0<span class=\"keyword\">void<\/span>\u00a0main(String\u00a0args[]){<\/li>\n<li class=\"\">\u00a0\u00a0\u00a0Simple\u00a0obj=<span class=\"keyword\">new<\/span>\u00a0Simple();<\/li>\n<li class=\"alt\">\u00a0\u00a0\u00a0obj.msg();<\/li>\n<li class=\"\">\u00a0\u00a0\u00a0}<\/li>\n<li class=\"alt\">}<\/li>\n<\/ol>\n<\/div>\n<\/div>\n<p>The default modifier is more restrictive than protected. That is why, there is a compile-time error.<\/p>\n<h2><span class=\"ez-toc-section\" id=\"Protected_Access_Modifier_%E2%80%93_Protected\"><\/span><strong>Protected Access Modifier &#8211; Protected<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Variables, methods, and constructors, which are declared protected in a superclass can be accessed only by the subclasses in other package or any class within the package of the protected members&#8217; class.<\/p>\n<p>The protected access modifier cannot be applied to class and interfaces. Methods, fields can be declared protected, however methods and fields in a interface cannot be declared protected.<\/p>\n<p>Protected access gives the subclass a chance to use the helper method or variable, while preventing a nonrelated class from trying to use it.<\/p>\n<h2><span class=\"ez-toc-section\" id=\"Example\"><\/span>Example<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>The following parent class uses protected access control, to allow its child class override\u00a0<em>openSpeaker()<\/em>\u00a0method \u2212<\/p>\n<pre class=\"prettyprint notranslate prettyprinted\"><span class=\"kwd\">class<\/span> <span class=\"typ\">AudioPlayer<\/span> <span class=\"pun\">{<\/span><span class=\"pln\">\r\n\u00a0 \u00a0<\/span><span class=\"kwd\">protected<\/span> <span class=\"kwd\">boolean<\/span><span class=\"pln\"> openSpeaker<\/span><span class=\"pun\">(<\/span><span class=\"typ\">Speaker<\/span><span class=\"pln\"> sp<\/span><span class=\"pun\">)<\/span> <span class=\"pun\">{<\/span><span class=\"pln\">\r\n\u00a0 \u00a0 \u00a0 <\/span><span class=\"com\">\/\/ implementation details<\/span><span class=\"pln\">\r\n\u00a0 \u00a0<\/span><span class=\"pun\">}<\/span>\r\n<span class=\"pun\">}<\/span>\r\n\r\n<span class=\"kwd\">class<\/span> <span class=\"typ\">StreamingAudioPlayer<\/span> <span class=\"pun\">{<\/span><span class=\"pln\">\r\n\u00a0 \u00a0<\/span><span class=\"kwd\">boolean<\/span><span class=\"pln\"> openSpeaker<\/span><span class=\"pun\">(<\/span><span class=\"typ\">Speaker<\/span><span class=\"pln\"> sp<\/span><span class=\"pun\">)<\/span> <span class=\"pun\">{<\/span><span class=\"pln\">\r\n\u00a0 \u00a0 \u00a0 <\/span><span class=\"com\">\/\/ implementation details<\/span><span class=\"pln\">\r\n\u00a0 \u00a0<\/span><span class=\"pun\">}<\/span>\r\n<span class=\"pun\">}<\/span><\/pre>\n<p>Here, if we define openSpeaker() method as private, then it would not be accessible from any other class other than\u00a0<em>AudioPlayer<\/em>. If we define it as public, then it would become accessible to all the outside world. But our intention is to expose this method to its subclass only, that&#8217;s why we have used protected modifier.<\/p>\n<h2><span class=\"ez-toc-section\" id=\"Introduction_to_Private_in_Java\"><\/span><strong>Introduction to Private in Java<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>The keyword \u2018private\u2019 in Java is used to establish the limitation of accessibility of the class, method or a variable in the java code block. If a class, method or variable is entitled as private in the program, that means that a particular class, method or variable cannot be accessed by outside the class or method, unlike the public method. The Private keyword is typically used in Java in a fully encapsulated class.<\/p>\n<h3><strong>How Private Keyword Works in Java?<\/strong><\/h3>\n<p>Private Keyword in Java works within a particular class. It can\u2019t be accessed outside of the class. It doesn\u2019t work outside the class\/classes and interface\/ interfaces. Private Keyword works well if the members of the class are PRIVATE and that too in a fully encapsulated class. Private Keyword or variable or method can also be overridden to the sub-class\/classes using some access modifiers to invoke PRIVATE METHOD outside of the class. With this, Private Keyword also works outside of the class only using the Private Access Modifiers.<\/p>\n<p style=\"text-align: center;\"><a href=\"https:\/\/bit.ly\/3ELmCiA\" target=\"_blank\" rel=\"noopener\"><strong>Learn to code from industry experts! Enroll here<\/strong><\/a><\/p>\n<h3>Examples<\/h3>\n<p>Here are some examples of private modifiers, which are as follows:<\/p>\n<h4>Example #1<\/h4>\n<p>Here we are illustrating the example of Private Access Modifier, which shows compilation error because of private data member accessing from the class AB, which is shown in the below example. Private methods or Private members can only be accessible within a particular class.<\/p>\n<p><b>Code:<\/b><\/p>\n<pre class=\"language-js\"><code class=\"language-js\"><span class=\"token keyword\">class<\/span> <span class=\"token class-name\">AB<\/span><span class=\"token punctuation\">{<\/span>\r\n<span class=\"token keyword\">private<\/span> double number <span class=\"token operator\">=<\/span> <span class=\"token number\">100<\/span><span class=\"token punctuation\">;<\/span>\r\n<span class=\"token keyword\">private<\/span> int <span class=\"token function\">squares<\/span><span class=\"token punctuation\">(<\/span><span class=\"token parameter\">int a<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">{<\/span>\r\n<span class=\"token keyword\">return<\/span> a<span class=\"token operator\">*<\/span>a<span class=\"token punctuation\">;<\/span>\r\n<span class=\"token punctuation\">}<\/span>\r\n<span class=\"token punctuation\">}<\/span>\r\n<span class=\"token keyword\">public<\/span> <span class=\"token keyword\">class<\/span> <span class=\"token class-name\">Main<\/span><span class=\"token punctuation\">{<\/span>\r\n<span class=\"token keyword\">public<\/span> <span class=\"token keyword\">static<\/span> <span class=\"token keyword\">void<\/span> <span class=\"token function\">main<\/span><span class=\"token punctuation\">(<\/span><span class=\"token parameter\">String args<span class=\"token punctuation\">[<\/span><span class=\"token punctuation\">]<\/span><\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">{<\/span>\r\n<span class=\"token constant\">AB<\/span> obj <span class=\"token operator\">=<\/span> <span class=\"token keyword\">new<\/span> <span class=\"token class-name\">AB<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span>\r\nSystem<span class=\"token punctuation\">.<\/span>out<span class=\"token punctuation\">.<\/span><span class=\"token function\">println<\/span><span class=\"token punctuation\">(<\/span>obj<span class=\"token punctuation\">.<\/span>number<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span>\r\nSystem<span class=\"token punctuation\">.<\/span>out<span class=\"token punctuation\">.<\/span><span class=\"token function\">println<\/span><span class=\"token punctuation\">(<\/span>obj<span class=\"token punctuation\">.<\/span><span class=\"token function\">squares<\/span><span class=\"token punctuation\">(<\/span><span class=\"token number\">10<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span>\r\n<span class=\"token punctuation\">}<\/span>\r\n<span class=\"token punctuation\">}<\/span><\/code><\/pre>\n<p><strong>Output:<\/strong><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-261143 lazyloaded\" src=\"https:\/\/cdn.educba.com\/academy\/wp-content\/uploads\/2019\/12\/Private-in-Java-eg1.png.webp\" srcset=\"https:\/\/cdn.educba.com\/academy\/wp-content\/uploads\/2019\/12\/Private-in-Java-eg1.png.webp 420w,https:\/\/cdn.educba.com\/academy\/wp-content\/uploads\/2019\/12\/Private-in-Java-eg1-300x88.png.webp 300w\" alt=\"Private in Java eg1\" width=\"420\" height=\"123\" data-ll-status=\"loaded\" \/><\/p>\n<h4>Example #2<\/h4>\n<p>This is an example of illustrating the use of the PRIVATE keyword with the program below:<\/p>\n<p><b>Code:<\/b><\/p>\n<pre class=\"language-js\"><code class=\"language-js\"><span class=\"token keyword\">class<\/span> <span class=\"token class-name\">Emp<\/span><span class=\"token punctuation\">{<\/span>\r\n<span class=\"token keyword\">private<\/span> int id1<span class=\"token punctuation\">;<\/span>\r\n<span class=\"token keyword\">private<\/span> String name14<span class=\"token punctuation\">;<\/span>\r\n<span class=\"token keyword\">private<\/span> int age14<span class=\"token punctuation\">;<\/span>\r\n<span class=\"token keyword\">public<\/span> int <span class=\"token function\">getId1<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token punctuation\">{<\/span>\r\n<span class=\"token keyword\">return<\/span> id1<span class=\"token punctuation\">;<\/span>\r\n<span class=\"token punctuation\">}<\/span>\r\n<span class=\"token keyword\">public<\/span> <span class=\"token keyword\">void<\/span> <span class=\"token function\">setId<\/span><span class=\"token punctuation\">(<\/span><span class=\"token parameter\">int id1<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token punctuation\">{<\/span>\r\n<span class=\"token keyword\">this<\/span><span class=\"token punctuation\">.<\/span>id1 <span class=\"token operator\">=<\/span> id1<span class=\"token punctuation\">;<\/span>\r\n<span class=\"token punctuation\">}<\/span>\r\n<span class=\"token keyword\">public<\/span> String <span class=\"token function\">getName14<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token punctuation\">{<\/span>\r\n<span class=\"token keyword\">return<\/span> name14<span class=\"token punctuation\">;<\/span>\r\n<span class=\"token punctuation\">}<\/span>\r\n<span class=\"token keyword\">public<\/span> <span class=\"token keyword\">void<\/span> <span class=\"token function\">setName14<\/span><span class=\"token punctuation\">(<\/span><span class=\"token parameter\">String name14<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token punctuation\">{<\/span>\r\n<span class=\"token keyword\">this<\/span><span class=\"token punctuation\">.<\/span>name14 <span class=\"token operator\">=<\/span> name14<span class=\"token punctuation\">;<\/span>\r\n<span class=\"token punctuation\">}<\/span>\r\n<span class=\"token keyword\">public<\/span> int <span class=\"token function\">getAge14<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token punctuation\">{<\/span>\r\n<span class=\"token keyword\">return<\/span> age14<span class=\"token punctuation\">;<\/span>\r\n<span class=\"token punctuation\">}<\/span>\r\n<span class=\"token keyword\">public<\/span> <span class=\"token keyword\">void<\/span> <span class=\"token function\">setAge14<\/span><span class=\"token punctuation\">(<\/span><span class=\"token parameter\">int age14<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token punctuation\">{<\/span>\r\n<span class=\"token keyword\">this<\/span><span class=\"token punctuation\">.<\/span>age14 <span class=\"token operator\">=<\/span> age14<span class=\"token punctuation\">;<\/span>\r\n<span class=\"token punctuation\">}<\/span>\r\n<span class=\"token punctuation\">}<\/span>\r\n<span class=\"token keyword\">public<\/span> <span class=\"token keyword\">class<\/span> <span class=\"token class-name\">Main<\/span><span class=\"token punctuation\">{<\/span>\r\n<span class=\"token keyword\">public<\/span> <span class=\"token keyword\">static<\/span> <span class=\"token keyword\">void<\/span> <span class=\"token function\">main<\/span><span class=\"token punctuation\">(<\/span><span class=\"token parameter\">String args<span class=\"token punctuation\">[<\/span><span class=\"token punctuation\">]<\/span><\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">{<\/span>\r\nEmp e<span class=\"token operator\">=<\/span><span class=\"token keyword\">new<\/span> <span class=\"token class-name\">Emp<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span>\r\ne<span class=\"token punctuation\">.<\/span><span class=\"token function\">setId<\/span><span class=\"token punctuation\">(<\/span><span class=\"token number\">1473<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span>\r\ne<span class=\"token punctuation\">.<\/span><span class=\"token function\">setName14<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string\">\"Pavan Kumar Sake\"<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span>\r\ne<span class=\"token punctuation\">.<\/span><span class=\"token function\">setAge14<\/span><span class=\"token punctuation\">(<\/span><span class=\"token number\">24<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span>\r\nSystem<span class=\"token punctuation\">.<\/span>out<span class=\"token punctuation\">.<\/span><span class=\"token function\">println<\/span><span class=\"token punctuation\">(<\/span>e<span class=\"token punctuation\">.<\/span><span class=\"token function\">getId1<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token operator\">+<\/span><span class=\"token string\">\" \"<\/span><span class=\"token operator\">+<\/span>e<span class=\"token punctuation\">.<\/span><span class=\"token function\">getName14<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token operator\">+<\/span><span class=\"token string\">\" \"<\/span><span class=\"token operator\">+<\/span>e<span class=\"token punctuation\">.<\/span><span class=\"token function\">getAge14<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span>\r\n<span class=\"token punctuation\">}<\/span>\r\n<span class=\"token punctuation\">}<\/span><\/code><\/pre>\n<p><strong>Output:<\/strong><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-261148 lazyloaded\" src=\"https:\/\/cdn.educba.com\/academy\/wp-content\/uploads\/2019\/12\/Private-in-Java-eg2.png.webp\" alt=\"Private in Java eg2\" width=\"200\" height=\"19\" data-ll-status=\"loaded\" \/><\/p>\n<h4>Example #3<\/h4>\n<p>Here in this example, you can see how the PRIVATE METHOD is overridden to the sub-class using the access modifier, which is the default. We are not even allowed to invoke the parent class method\/methods from the sub-class.<\/p>\n<p><b>Code:<\/b><\/p>\n<pre class=\"language-js\"><code class=\"language-js\"><span class=\"token keyword\">class<\/span> <span class=\"token class-name\">ABC<\/span><span class=\"token punctuation\">{<\/span>\r\n<span class=\"token keyword\">private<\/span> <span class=\"token keyword\">void<\/span> <span class=\"token function\">msg<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span>\r\n<span class=\"token punctuation\">{<\/span>\r\nSystem<span class=\"token punctuation\">.<\/span>out<span class=\"token punctuation\">.<\/span><span class=\"token function\">println<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string\">\"Hey Buddy this is the parent class method\"<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span> <span class=\"token comment\">\/\/Output of the Private Method<\/span>\r\n<span class=\"token punctuation\">}<\/span>\r\n<span class=\"token punctuation\">}<\/span>\r\n<span class=\"token keyword\">public<\/span> <span class=\"token keyword\">class<\/span> <span class=\"token class-name\">Main<\/span> <span class=\"token keyword\">extends<\/span> <span class=\"token class-name\">ABC<\/span><span class=\"token punctuation\">{<\/span> <span class=\"token comment\">\/\/ Private method is overridden using the access modifier<\/span>\r\n<span class=\"token keyword\">void<\/span> <span class=\"token function\">msg<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span>\r\n<span class=\"token punctuation\">{<\/span>\r\nSystem<span class=\"token punctuation\">.<\/span>out<span class=\"token punctuation\">.<\/span><span class=\"token function\">println<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string\">\"Buddy this is the child class method\"<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span>\r\n<span class=\"token punctuation\">}<\/span>\r\n<span class=\"token keyword\">public<\/span> <span class=\"token keyword\">static<\/span> <span class=\"token keyword\">void<\/span> <span class=\"token function\">main<\/span><span class=\"token punctuation\">(<\/span><span class=\"token parameter\">String args<span class=\"token punctuation\">[<\/span><span class=\"token punctuation\">]<\/span><\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">{<\/span>\r\nMain obj<span class=\"token operator\">=<\/span><span class=\"token keyword\">new<\/span> <span class=\"token class-name\">Main<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span>\r\nobj<span class=\"token punctuation\">.<\/span><span class=\"token function\">msg<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span>\r\n<span class=\"token punctuation\">}<\/span>\r\n<span class=\"token punctuation\">}<\/span><\/code><\/pre>\n<p><strong>Output:<\/strong><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-261151 lazyloaded\" src=\"https:\/\/cdn.educba.com\/academy\/wp-content\/uploads\/2019\/12\/Private-in-Java-eg3.png.webp\" srcset=\"https:\/\/cdn.educba.com\/academy\/wp-content\/uploads\/2019\/12\/Private-in-Java-eg3.png.webp 304w,https:\/\/cdn.educba.com\/academy\/wp-content\/uploads\/2019\/12\/Private-in-Java-eg3-300x17.png.webp 300w\" alt=\"Private in Java eg3\" width=\"304\" height=\"17\" data-ll-status=\"loaded\" \/><\/p>\n<h2><span class=\"ez-toc-section\" id=\"Rules_and_Regulations_for_Private_Constructor_in_Java\"><\/span><strong>Rules and Regulations for\u00a0Private Constructor in Java<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Here are some rules and regulations for private that you should know.<\/p>\n<ul>\n<li>If the class contains\u00a0PRIVATE CONSTRUCTOR, you can\u2019t create any object of that particular class from the outside class.<\/li>\n<li>You should not access a private keyword outside of the class. If you try, it will produce a compilation error for that deed. For that, you have to modify the code.<\/li>\n<li>Top-level classes can\u2019t be private in this java language.<\/li>\n<\/ul>\n<h4 style=\"text-align: center;\"><a href=\"https:\/\/bit.ly\/3ELmCiA\" target=\"_blank\" rel=\"noopener\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-25522670 size-full\" src=\"https:\/\/entri.app\/blog\/wp-content\/uploads\/2022\/04\/Python-and-Machine-Learning-Rectangle-1.png\" alt=\"Python and Machine Learning Rectangle\" width=\"970\" height=\"250\" srcset=\"https:\/\/entri.app\/blog\/wp-content\/uploads\/2022\/04\/Python-and-Machine-Learning-Rectangle-1.png 970w, https:\/\/entri.app\/blog\/wp-content\/uploads\/2022\/04\/Python-and-Machine-Learning-Rectangle-1-300x77.png 300w, https:\/\/entri.app\/blog\/wp-content\/uploads\/2022\/04\/Python-and-Machine-Learning-Rectangle-1-768x198.png 768w, https:\/\/entri.app\/blog\/wp-content\/uploads\/2022\/04\/Python-and-Machine-Learning-Rectangle-1-750x193.png 750w\" sizes=\"auto, (max-width: 970px) 100vw, 970px\" \/><\/a><\/h4>\n","protected":false},"excerpt":{"rendered":"<p>There are two types of modifiers in Java:\u00a0access modifiers\u00a0and\u00a0non-access modifiers. The access modifiers in Java specifies the accessibility or scope of a field, method, constructor, or class. We can change the access level of fields, constructors, methods, and class by applying the access modifier on it. There are four types of Java access modifiers: Private: [&hellip;]<\/p>\n","protected":false},"author":111,"featured_media":25545517,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[802,1882],"tags":[],"class_list":["post-25545513","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-articles","category-java-programming"],"acf":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.6 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>What is Access Modifiers in Java- A Short Note - Entri Blog<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/entri.app\/blog\/what-is-access-modifiers-in-java-a-short-note\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"What is Access Modifiers in Java- A Short Note - Entri Blog\" \/>\n<meta property=\"og:description\" content=\"There are two types of modifiers in Java:\u00a0access modifiers\u00a0and\u00a0non-access modifiers. The access modifiers in Java specifies the accessibility or scope of a field, method, constructor, or class. We can change the access level of fields, constructors, methods, and class by applying the access modifier on it. There are four types of Java access modifiers: Private: [&hellip;]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/entri.app\/blog\/what-is-access-modifiers-in-java-a-short-note\/\" \/>\n<meta property=\"og:site_name\" content=\"Entri Blog\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/entri.me\/\" \/>\n<meta property=\"article:published_time\" content=\"2022-10-23T07:59:15+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2022-11-23T08:48:34+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/entri.app\/blog\/wp-content\/uploads\/2022\/10\/What-is-Access-Modifiers-in-Java-A-Short-Note-1.png\" \/>\n\t<meta property=\"og:image:width\" content=\"820\" \/>\n\t<meta property=\"og:image:height\" content=\"615\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"author\" content=\"Feeba Mahin\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@entri_app\" \/>\n<meta name=\"twitter:site\" content=\"@entri_app\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Feeba Mahin\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"8 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/entri.app\/blog\/what-is-access-modifiers-in-java-a-short-note\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/entri.app\/blog\/what-is-access-modifiers-in-java-a-short-note\/\"},\"author\":{\"name\":\"Feeba Mahin\",\"@id\":\"https:\/\/entri.app\/blog\/#\/schema\/person\/f036dab84abae3dcc9390a1110d95d36\"},\"headline\":\"What is Access Modifiers in Java- A Short Note\",\"datePublished\":\"2022-10-23T07:59:15+00:00\",\"dateModified\":\"2022-11-23T08:48:34+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/entri.app\/blog\/what-is-access-modifiers-in-java-a-short-note\/\"},\"wordCount\":1410,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/entri.app\/blog\/#organization\"},\"image\":{\"@id\":\"https:\/\/entri.app\/blog\/what-is-access-modifiers-in-java-a-short-note\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/entri.app\/blog\/wp-content\/uploads\/2022\/10\/What-is-Access-Modifiers-in-Java-A-Short-Note-1.png\",\"articleSection\":[\"Articles\",\"Java Programming\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/entri.app\/blog\/what-is-access-modifiers-in-java-a-short-note\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/entri.app\/blog\/what-is-access-modifiers-in-java-a-short-note\/\",\"url\":\"https:\/\/entri.app\/blog\/what-is-access-modifiers-in-java-a-short-note\/\",\"name\":\"What is Access Modifiers in Java- A Short Note - Entri Blog\",\"isPartOf\":{\"@id\":\"https:\/\/entri.app\/blog\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/entri.app\/blog\/what-is-access-modifiers-in-java-a-short-note\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/entri.app\/blog\/what-is-access-modifiers-in-java-a-short-note\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/entri.app\/blog\/wp-content\/uploads\/2022\/10\/What-is-Access-Modifiers-in-Java-A-Short-Note-1.png\",\"datePublished\":\"2022-10-23T07:59:15+00:00\",\"dateModified\":\"2022-11-23T08:48:34+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/entri.app\/blog\/what-is-access-modifiers-in-java-a-short-note\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/entri.app\/blog\/what-is-access-modifiers-in-java-a-short-note\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/entri.app\/blog\/what-is-access-modifiers-in-java-a-short-note\/#primaryimage\",\"url\":\"https:\/\/entri.app\/blog\/wp-content\/uploads\/2022\/10\/What-is-Access-Modifiers-in-Java-A-Short-Note-1.png\",\"contentUrl\":\"https:\/\/entri.app\/blog\/wp-content\/uploads\/2022\/10\/What-is-Access-Modifiers-in-Java-A-Short-Note-1.png\",\"width\":820,\"height\":615,\"caption\":\"What is Access Modifiers in Java- A Short Note\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/entri.app\/blog\/what-is-access-modifiers-in-java-a-short-note\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/entri.app\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Entri Skilling\",\"item\":\"https:\/\/entri.app\/blog\/category\/entri-skilling\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"Java Programming\",\"item\":\"https:\/\/entri.app\/blog\/category\/entri-skilling\/java-programming\/\"},{\"@type\":\"ListItem\",\"position\":4,\"name\":\"What is Access Modifiers in Java- A Short Note\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/entri.app\/blog\/#website\",\"url\":\"https:\/\/entri.app\/blog\/\",\"name\":\"Entri Blog\",\"description\":\"\",\"publisher\":{\"@id\":\"https:\/\/entri.app\/blog\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/entri.app\/blog\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/entri.app\/blog\/#organization\",\"name\":\"Entri App\",\"url\":\"https:\/\/entri.app\/blog\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/entri.app\/blog\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/entri.app\/blog\/wp-content\/uploads\/2019\/10\/Entri-Logo-1.png\",\"contentUrl\":\"https:\/\/entri.app\/blog\/wp-content\/uploads\/2019\/10\/Entri-Logo-1.png\",\"width\":989,\"height\":446,\"caption\":\"Entri App\"},\"image\":{\"@id\":\"https:\/\/entri.app\/blog\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/www.facebook.com\/entri.me\/\",\"https:\/\/x.com\/entri_app\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/entri.app\/blog\/#\/schema\/person\/f036dab84abae3dcc9390a1110d95d36\",\"name\":\"Feeba Mahin\",\"url\":\"https:\/\/entri.app\/blog\/author\/feeba123\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"What is Access Modifiers in Java- A Short Note - Entri Blog","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/entri.app\/blog\/what-is-access-modifiers-in-java-a-short-note\/","og_locale":"en_US","og_type":"article","og_title":"What is Access Modifiers in Java- A Short Note - Entri Blog","og_description":"There are two types of modifiers in Java:\u00a0access modifiers\u00a0and\u00a0non-access modifiers. The access modifiers in Java specifies the accessibility or scope of a field, method, constructor, or class. We can change the access level of fields, constructors, methods, and class by applying the access modifier on it. There are four types of Java access modifiers: Private: [&hellip;]","og_url":"https:\/\/entri.app\/blog\/what-is-access-modifiers-in-java-a-short-note\/","og_site_name":"Entri Blog","article_publisher":"https:\/\/www.facebook.com\/entri.me\/","article_published_time":"2022-10-23T07:59:15+00:00","article_modified_time":"2022-11-23T08:48:34+00:00","og_image":[{"width":820,"height":615,"url":"https:\/\/entri.app\/blog\/wp-content\/uploads\/2022\/10\/What-is-Access-Modifiers-in-Java-A-Short-Note-1.png","type":"image\/png"}],"author":"Feeba Mahin","twitter_card":"summary_large_image","twitter_creator":"@entri_app","twitter_site":"@entri_app","twitter_misc":{"Written by":"Feeba Mahin","Est. reading time":"8 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/entri.app\/blog\/what-is-access-modifiers-in-java-a-short-note\/#article","isPartOf":{"@id":"https:\/\/entri.app\/blog\/what-is-access-modifiers-in-java-a-short-note\/"},"author":{"name":"Feeba Mahin","@id":"https:\/\/entri.app\/blog\/#\/schema\/person\/f036dab84abae3dcc9390a1110d95d36"},"headline":"What is Access Modifiers in Java- A Short Note","datePublished":"2022-10-23T07:59:15+00:00","dateModified":"2022-11-23T08:48:34+00:00","mainEntityOfPage":{"@id":"https:\/\/entri.app\/blog\/what-is-access-modifiers-in-java-a-short-note\/"},"wordCount":1410,"commentCount":0,"publisher":{"@id":"https:\/\/entri.app\/blog\/#organization"},"image":{"@id":"https:\/\/entri.app\/blog\/what-is-access-modifiers-in-java-a-short-note\/#primaryimage"},"thumbnailUrl":"https:\/\/entri.app\/blog\/wp-content\/uploads\/2022\/10\/What-is-Access-Modifiers-in-Java-A-Short-Note-1.png","articleSection":["Articles","Java Programming"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/entri.app\/blog\/what-is-access-modifiers-in-java-a-short-note\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/entri.app\/blog\/what-is-access-modifiers-in-java-a-short-note\/","url":"https:\/\/entri.app\/blog\/what-is-access-modifiers-in-java-a-short-note\/","name":"What is Access Modifiers in Java- A Short Note - Entri Blog","isPartOf":{"@id":"https:\/\/entri.app\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/entri.app\/blog\/what-is-access-modifiers-in-java-a-short-note\/#primaryimage"},"image":{"@id":"https:\/\/entri.app\/blog\/what-is-access-modifiers-in-java-a-short-note\/#primaryimage"},"thumbnailUrl":"https:\/\/entri.app\/blog\/wp-content\/uploads\/2022\/10\/What-is-Access-Modifiers-in-Java-A-Short-Note-1.png","datePublished":"2022-10-23T07:59:15+00:00","dateModified":"2022-11-23T08:48:34+00:00","breadcrumb":{"@id":"https:\/\/entri.app\/blog\/what-is-access-modifiers-in-java-a-short-note\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/entri.app\/blog\/what-is-access-modifiers-in-java-a-short-note\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/entri.app\/blog\/what-is-access-modifiers-in-java-a-short-note\/#primaryimage","url":"https:\/\/entri.app\/blog\/wp-content\/uploads\/2022\/10\/What-is-Access-Modifiers-in-Java-A-Short-Note-1.png","contentUrl":"https:\/\/entri.app\/blog\/wp-content\/uploads\/2022\/10\/What-is-Access-Modifiers-in-Java-A-Short-Note-1.png","width":820,"height":615,"caption":"What is Access Modifiers in Java- A Short Note"},{"@type":"BreadcrumbList","@id":"https:\/\/entri.app\/blog\/what-is-access-modifiers-in-java-a-short-note\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/entri.app\/blog\/"},{"@type":"ListItem","position":2,"name":"Entri Skilling","item":"https:\/\/entri.app\/blog\/category\/entri-skilling\/"},{"@type":"ListItem","position":3,"name":"Java Programming","item":"https:\/\/entri.app\/blog\/category\/entri-skilling\/java-programming\/"},{"@type":"ListItem","position":4,"name":"What is Access Modifiers in Java- A Short Note"}]},{"@type":"WebSite","@id":"https:\/\/entri.app\/blog\/#website","url":"https:\/\/entri.app\/blog\/","name":"Entri Blog","description":"","publisher":{"@id":"https:\/\/entri.app\/blog\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/entri.app\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/entri.app\/blog\/#organization","name":"Entri App","url":"https:\/\/entri.app\/blog\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/entri.app\/blog\/#\/schema\/logo\/image\/","url":"https:\/\/entri.app\/blog\/wp-content\/uploads\/2019\/10\/Entri-Logo-1.png","contentUrl":"https:\/\/entri.app\/blog\/wp-content\/uploads\/2019\/10\/Entri-Logo-1.png","width":989,"height":446,"caption":"Entri App"},"image":{"@id":"https:\/\/entri.app\/blog\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/entri.me\/","https:\/\/x.com\/entri_app"]},{"@type":"Person","@id":"https:\/\/entri.app\/blog\/#\/schema\/person\/f036dab84abae3dcc9390a1110d95d36","name":"Feeba Mahin","url":"https:\/\/entri.app\/blog\/author\/feeba123\/"}]}},"_links":{"self":[{"href":"https:\/\/entri.app\/blog\/wp-json\/wp\/v2\/posts\/25545513","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/entri.app\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/entri.app\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/entri.app\/blog\/wp-json\/wp\/v2\/users\/111"}],"replies":[{"embeddable":true,"href":"https:\/\/entri.app\/blog\/wp-json\/wp\/v2\/comments?post=25545513"}],"version-history":[{"count":7,"href":"https:\/\/entri.app\/blog\/wp-json\/wp\/v2\/posts\/25545513\/revisions"}],"predecessor-version":[{"id":25547992,"href":"https:\/\/entri.app\/blog\/wp-json\/wp\/v2\/posts\/25545513\/revisions\/25547992"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/entri.app\/blog\/wp-json\/wp\/v2\/media\/25545517"}],"wp:attachment":[{"href":"https:\/\/entri.app\/blog\/wp-json\/wp\/v2\/media?parent=25545513"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/entri.app\/blog\/wp-json\/wp\/v2\/categories?post=25545513"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/entri.app\/blog\/wp-json\/wp\/v2\/tags?post=25545513"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}