{"id":9862,"date":"2014-09-19T10:00:13","date_gmt":"2014-09-19T10:00:13","guid":{"rendered":"https:\/\/www.whizlabs.com\/?p=9862"},"modified":"2020-09-01T07:18:57","modified_gmt":"2020-09-01T07:18:57","slug":"java-access-modifiers-ii","status":"publish","type":"post","link":"https:\/\/www.whizlabs.com\/blog\/java-access-modifiers-ii\/","title":{"rendered":"Java Access Modifiers \u2013 II"},"content":{"rendered":"<p>The concept of \u2018access modifiers\u2019, understanding them and working with them are some of the key objectives of the SCJP\/OCJP exam. It is also important to understand this concept to excel as a Java developer as well.\u00a0 We have already started our discussion of \u2018Access modifiers\u2019 in our earlier post. We will continue our discussion by extending it to \u2018private access modifier\u2019.<\/p>\n<p><b>\u2018private modifier\u2019:<\/b><\/p>\n<p>Only methods and variables can use the \u2018private access\u2019 modifier\u2019. The \u2018private access modifier\u2019 is the most restrictive access modifier.<\/p>\n<p>One of the interesting ways to think about the \u2018private access modifier\u2019 is to view it as \u201cinvisible\u201d. If the variables and methods are declared \u201cprivate\u201d, they are \u201cinvisible\u201d and hence they cannot be accessed in any other class or sub-classes. This brings us to the first of the three key points that we will discuss:<\/p>\n<p><strong>1.<\/strong> \u2018<b>private\u2019 variables and methods can only be accessed in the class where it was declared.<\/b><\/p>\n<p>As an example, consider this program:<\/p>\n<p><span style=\"color: #003366\">example3.java:<\/span><\/p>\n<p><span style=\"color: #003366\"><b>package<\/b> whizlabs;<\/span><\/p>\n<p><span style=\"color: #003366\">\u00a0<b>\u00a0public<\/b> <b>class<\/b> example3 {<\/span><br \/>\n<span style=\"color: #003366\">\u00a0 \u00a0 \u00a0 \u00a0<b>private<\/b> <b>int<\/b> i,j;<br \/>\n<\/span><span style=\"color: #003366\"><b>\u00a0 \u00a0 \u00a0 \u00a0private<\/b> <b>void<\/b> add_1(){<\/span><br \/>\n<span style=\"color: #003366\">\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 System.<i>out<\/i>.println(&#8220;Hello world&#8221;);<\/span><br \/>\n<span style=\"color: #003366\">\u00a0 \u00a0 \u00a0 \u00a0}<\/span><\/p>\n<p><span style=\"color: #003366\"><span style=\"line-height: 1.5em\">\u00a0 \u00a0 \u00a0<\/span><b style=\"line-height: 1.5em\">public\u00a0<\/b><b style=\"line-height: 1.5em\">static\u00a0<\/b><b style=\"line-height: 1.5em\">void<\/b><span style=\"line-height: 1.5em\"> main(String[] args) {<br \/>\n<\/span>\u00a0 \u00a0 \u00a0 \u00a0}<br \/>\n<\/span><span style=\"line-height: 1.5em;color: #003366\">}<\/span><\/p>\n<p><span style=\"color: #003366\"><b style=\"line-height: 1.5em\">class<\/b><span style=\"line-height: 1.5em\"> example4{<br \/>\n<\/span>\u00a0 \u00a0 \u00a0 \u00a0example3 e=<b>new<\/b> example3();<br \/>\n<\/span><span style=\"color: #003366\">\u00a0 \u00a0 \u00a0 \u00a0e.<span style=\"text-decoration: underline\">add_1<\/span>();<br \/>\n<\/span><span style=\"color: #003366\">}<\/span><\/p>\n<p><span style=\"line-height: 1.5em\">The above code will cause a compiler error since add_1() is being accessed in another class other than the class where it was declared. This will be the output on the console.<\/span><\/p>\n<p><span style=\"color: #ff0000\"><span style=\"line-height: 1.5em\">Exception in thread &#8220;main&#8221; java.lang.Error: Unresolved compilation problem:<br \/>\n<\/span>\u00a0 \u00a0 \u00a0 \u00a0at whizlabs.example3.main(<span style=\"text-decoration: underline\">example3.java:9<\/span>)<\/span><\/p>\n<p><span style=\"line-height: 1.5em\">Declaring it this way will rectify the error:<\/span><\/p>\n<p><b>package<\/b> whizlabs;<\/p>\n<p><b style=\"line-height: 1.5em\">public\u00a0<\/b><b style=\"line-height: 1.5em\">class<\/b><span style=\"line-height: 1.5em\"> example3 {<br \/>\n<\/span>\u00a0 \u00a0 \u00a0 \u00a0<b>private<\/b> <b>int<\/b> <span style=\"text-decoration: underline\">i<\/span>,<span style=\"text-decoration: underline\">j<\/span>;<br \/>\n<b>\u00a0 \u00a0 \u00a0 \u00a0private<\/b> <b>void<\/b> add_1(){<\/p>\n<p>System.<i>out<\/i>.println(&#8220;Hello world&#8221;);<\/p>\n<p>}<\/p>\n<p><b style=\"line-height: 1.5em\">\u00a0 \u00a0 \u00a0 \u00a0public\u00a0<\/b><b style=\"line-height: 1.5em\">static\u00a0<\/b><b style=\"line-height: 1.5em\">void<\/b><span style=\"line-height: 1.5em\"> main(String[] args) {<br \/>\n<\/span>\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 example3 e=<b>new<\/b> example3(); \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0\/\/code has been moved here<\/p>\n<p style=\"padding-left: 30px\">e.add_1();<\/p>\n<p>\u00a0 \u00a0 }<br \/>\n<span style=\"line-height: 1.5em\">}<\/span><\/p>\n<p>&nbsp;<\/p>\n<p><b>class<\/b> example4{<br \/>\n\/\/ An empty class declaration<br \/>\n}<\/p>\n<p>&nbsp;<\/p>\n<p><b>2.\u00a0\u00a0\u00a0\u00a0 <\/b>\u00a0<b>The \u201cinvisible\u201d private methods and variables will not be inherited by the sub-classes.<\/b><\/p>\n<p><span style=\"line-height: 1.5em\">Consider this code: example3.java:<\/span><\/p>\n<p><b>package<\/b> whizlabs;<\/p>\n<p><b style=\"line-height: 1.5em\">public\u00a0<\/b><b style=\"line-height: 1.5em\">class<\/b><span style=\"line-height: 1.5em\"> example3 {<br \/>\n<\/span>\u00a0 \u00a0 \u00a0 \u00a0<b>private<\/b> <b>int<\/b> <span style=\"text-decoration: underline\">i<\/span>,<span style=\"text-decoration: underline\">j<\/span>;<br \/>\n<b>\u00a0 \u00a0 \u00a0 \u00a0private<\/b> <b>void<\/b> add_1(){<\/p>\n<p>System.<i>out<\/i>.println(&#8220;Hello world&#8221;);<br \/>\n}<\/p>\n<p><span style=\"line-height: 1.5em\">\u00a0 \u00a0<\/span><b style=\"line-height: 1.5em\">public\u00a0<\/b><b style=\"line-height: 1.5em\">static\u00a0<\/b><b style=\"line-height: 1.5em\">void<\/b><span style=\"line-height: 1.5em\"> main(String[] args) {<br \/>\n<\/span>\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 example3 e=<b>new<\/b> example3();<\/p>\n<p>e.add_1();<br \/>\n}<br \/>\n}<\/p>\n<p><span style=\"line-height: 1.5em\">Next consider this code: example5.java:<\/span><\/p>\n<p><b style=\"line-height: 1.5em\">package<\/b><span style=\"line-height: 1.5em\"> whizlabs;<\/span><\/p>\n<p><b style=\"line-height: 1.5em\">public\u00a0<\/b><b style=\"line-height: 1.5em\">class<\/b><span style=\"line-height: 1.5em\"> example5 <\/span><b style=\"line-height: 1.5em\">extends<\/b><span style=\"line-height: 1.5em\"> example3 {<br \/>\n<\/span>\u00a0 \u00a0 \u00a0 \u00a0<b>public<\/b> <b>void<\/b> test(){<\/p>\n<p><span style=\"text-decoration: underline\">add_1<\/span>();<br \/>\n}<\/p>\n<p><span style=\"line-height: 1.5em\">\u00a0\u00a0<\/span><b style=\"line-height: 1.5em\">public\u00a0<\/b><b style=\"line-height: 1.5em\">static\u00a0<\/b><b style=\"line-height: 1.5em\">void<\/b><span style=\"line-height: 1.5em\"> main(String[] args) {<br \/>\n<\/span>\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0}<br \/>\n}<\/p>\n<p><span style=\"line-height: 1.5em\">example5.java is a sub-class of example3.java and both of them are in the same package \u2018whizlabs\u2019.<\/span><\/p>\n<p>Even though example5.java is a sub-class of example3.java, method add_1() is \u201cprivate\u201d and it will be invisible in \u2018example5.java\u2019.<\/p>\n<p><span style=\"line-height: 1.5em\">\u00a0This will be the error that will be encountered:<\/span><\/p>\n<p><span style=\"color: #ff0000\"><em><span style=\"line-height: 1.5em\">\u201cThe method add_1() from the type example3 is not visible\u201d<\/span><\/em><\/span><\/p>\n<p>Changing the visibility from \u2018<span style=\"color: #ff0000\"><b>private<\/b><\/span>\u2019 to \u2018<span style=\"color: #ff0000\"><b>public<\/b><\/span>\u2019 for the add_1() method in \u2018example3.java\u2019, will eliminate this error.<\/p>\n<p><b>\u00a0public void<\/b> add_1(){<br \/>\nSystem.<i>out<\/i>.println(&#8220;Hello world&#8221;);<br \/>\n}<\/p>\n<p><strong>\u00a03.\u00a0<\/strong>\u2018<b>private\u2019 methods cannot be overridden by inherited sub-classes<\/b>.<\/p>\n<p>Put simply, if a method is \u201cinvisible\u201d (private) how can it be overridden by sub-classes?\u00a0 If a private method is declared with the same signature as its super-class, it is just considered as \u201canother method\u201d and not as an overridden method.<\/p>\n<p><span style=\"line-height: 1.5em\">Consider the code below: example3.java:<\/span><\/p>\n<p><b>package<\/b> whizlabs;<\/p>\n<p><b style=\"line-height: 1.5em\">public\u00a0<\/b><b style=\"line-height: 1.5em\">class<\/b><span style=\"line-height: 1.5em\"> example3 {<br \/>\n<\/span>\u00a0 \u00a0 \u00a0 \u00a0<b>private<\/b> <b>int<\/b> <span style=\"text-decoration: underline\">i<\/span>,<span style=\"text-decoration: underline\">j<\/span>;<br \/>\n<b>\u00a0 \u00a0 \u00a0 \u00a0private<\/b> <b>void<\/b> <span style=\"text-decoration: underline\">add_1()<\/span>{<\/p>\n<p>System.<i>out<\/i>.println(&#8220;Hello world&#8221;);<br \/>\n}<\/p>\n<p><span style=\"line-height: 1.5em\">\u00a0\u00a0<\/span><b style=\"line-height: 1.5em\">public\u00a0<\/b><b style=\"line-height: 1.5em\">static\u00a0<\/b><b style=\"line-height: 1.5em\">void<\/b><span style=\"line-height: 1.5em\"> main(String[] args) {<\/span><br \/>\n}<br \/>\n}<\/p>\n<p><span style=\"line-height: 1.5em\">example5.java:<\/span><\/p>\n<p><b>package<\/b> whizlabs;<\/p>\n<p><b style=\"line-height: 1.5em\">public\u00a0<\/b><b style=\"line-height: 1.5em\">class<\/b><span style=\"line-height: 1.5em\"> example5 <\/span><b style=\"line-height: 1.5em\">extends<\/b><span style=\"line-height: 1.5em\"> example3 {<br \/>\n<\/span>\u00a0 \u00a0 \u00a0 \u00a0<b>private<\/b> <b>void<\/b> <span style=\"text-decoration: underline\">add_1();<\/span><\/p>\n<p>System.<i>out<\/i>.println(&#8220;Hello!!&#8221;);<\/p>\n<p>}<\/p>\n<p><b>\u00a0public<\/b> <b>static<\/b> <b>void<\/b> main(String[] args) {<br \/>\nexample5 e=<b>new<\/b> example5();<br \/>\ne.<span style=\"text-decoration: underline\">add_1<\/span>();<br \/>\n}<br \/>\n}<\/p>\n<p>This will give us the following output on the console:<\/p>\n<p>Hello!!<\/p>\n<p>&nbsp;<\/p>\n<p>In the above example, we can see that even though the method add_1() has been defined again in \u2018example5.java\u2019 &#8211; it is not overriding. It is just considered as another method and the result \u201cHello!!\u201d is printed out.<\/p>\n<p><span style=\"line-height: 1.5em\">We have seen the \u2018private\u2019 access modifiers and some key points related to it in this post. We will extend our discussion of the access modifiers in the next post.<\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>The concept of \u2018access modifiers\u2019, understanding them and working with them are some of the key objectives of the SCJP\/OCJP exam. It is also important to understand this concept to excel as a Java developer as well.\u00a0 We have already started our discussion of \u2018Access modifiers\u2019 in our earlier post. We will continue our discussion by extending it to \u2018private access modifier\u2019. \u2018private modifier\u2019: Only methods and variables can use the \u2018private access\u2019 modifier\u2019. The \u2018private access modifier\u2019 is the most restrictive access modifier. One of the interesting ways to think about the \u2018private access modifier\u2019 is to view it [&hellip;]<\/p>\n","protected":false},"author":220,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_uag_custom_page_level_css":"","site-sidebar-layout":"default","site-content-layout":"","ast-site-content-layout":"default","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"default","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"footnotes":""},"categories":[13],"tags":[969],"class_list":["post-9862","post","type-post","status-publish","format-standard","hentry","category-java","tag-java-access-modifiers"],"uagb_featured_image_src":{"full":false,"thumbnail":false,"medium":false,"medium_large":false,"large":false,"1536x1536":false,"2048x2048":false,"profile_24":false,"profile_48":false,"profile_96":false,"profile_150":false,"profile_300":false,"tptn_thumbnail":false,"web-stories-poster-portrait":false,"web-stories-publisher-logo":false,"web-stories-thumbnail":false},"uagb_author_info":{"display_name":"Aditi Malhotra","author_link":"https:\/\/www.whizlabs.com\/blog\/author\/aditi\/"},"uagb_comment_info":0,"uagb_excerpt":"The concept of \u2018access modifiers\u2019, understanding them and working with them are some of the key objectives of the SCJP\/OCJP exam. It is also important to understand this concept to excel as a Java developer as well.\u00a0 We have already started our discussion of \u2018Access modifiers\u2019 in our earlier post. We will continue our discussion&hellip;","_links":{"self":[{"href":"https:\/\/www.whizlabs.com\/blog\/wp-json\/wp\/v2\/posts\/9862","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.whizlabs.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.whizlabs.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.whizlabs.com\/blog\/wp-json\/wp\/v2\/users\/220"}],"replies":[{"embeddable":true,"href":"https:\/\/www.whizlabs.com\/blog\/wp-json\/wp\/v2\/comments?post=9862"}],"version-history":[{"count":1,"href":"https:\/\/www.whizlabs.com\/blog\/wp-json\/wp\/v2\/posts\/9862\/revisions"}],"predecessor-version":[{"id":76000,"href":"https:\/\/www.whizlabs.com\/blog\/wp-json\/wp\/v2\/posts\/9862\/revisions\/76000"}],"wp:attachment":[{"href":"https:\/\/www.whizlabs.com\/blog\/wp-json\/wp\/v2\/media?parent=9862"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.whizlabs.com\/blog\/wp-json\/wp\/v2\/categories?post=9862"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.whizlabs.com\/blog\/wp-json\/wp\/v2\/tags?post=9862"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}