Site icon C1CTech

Java Access Modifiers

&NewLine;<p>This post is about <strong>Access&sol;Visibility modifiers in Java<&sol;strong> and how to use them&period; <&sol;p>&NewLine;&NewLine;&NewLine;&NewLine;<h3 class&equals;"wp-block-heading"><strong><span style&equals;"color&colon;&num;520599" class&equals;"has-inline-color">Access Modifiers<&sol;span><&sol;strong><&sol;h3>&NewLine;&NewLine;&NewLine;&NewLine;<p>In Java&comma; access modifiers are used to set the accessibility &lpar;visibility&rpar; of <strong>classes&comma; constructors&comma; data members <&sol;strong>and<strong> methods<&sol;strong> in another classes&period;&nbsp&semi;<&sol;p>&NewLine;&NewLine;&NewLine;&NewLine;<p>There are four types of access modifiers available in java&colon;<&sol;p>&NewLine;&NewLine;&NewLine;&NewLine;<ol class&equals;"wp-block-list"><li><strong><span style&equals;"color&colon;&num;0000ff" class&equals;"has-inline-color">Default<&sol;span><&sol;strong> &colon; visible only within the package&period;<&sol;li><li><strong><span style&equals;"color&colon;&num;0000ff" class&equals;"has-inline-color">Private<&sol;span><&sol;strong>&colon; visible within the class only&period;<&sol;li><li><strong><span style&equals;"color&colon;&num;0000ff" class&equals;"has-inline-color">Protected<&sol;span><&sol;strong>&colon; visible within the package or all subclasses &lpar;any package&rpar;&period;<&sol;li><li><strong><span style&equals;"color&colon;&num;0000ff" class&equals;"has-inline-color">Public<&sol;span><&sol;strong>&colon; visible everywhere in entire project&period;<&sol;li><&sol;ol>&NewLine;&NewLine;&NewLine;&NewLine;<p>Let&&num;8217&semi;s understand each modifier in detail with simple example&period;<&sol;p>&NewLine;&NewLine;&NewLine;&NewLine;<h3 class&equals;"wp-block-heading"><strong><span style&equals;"color&colon;&num;520599" class&equals;"has-inline-color">Default Access Modifier<&sol;span><&sol;strong><&sol;h3>&NewLine;&NewLine;&NewLine;&NewLine;<p>When no access modifier is specified&comma; its the&nbsp&semi;<strong><span style&equals;"color&colon;&num;04603e" class&equals;"has-inline-color">default<&sol;span><&sol;strong>&nbsp&semi;access modifier by default&period;<&sol;p>&NewLine;&NewLine;&NewLine;&NewLine;<p>The data members&comma; class or methods which are not declared using any access modifiers i&period;e&period; having default access modifier are accessible&nbsp&semi;<span style&equals;"color&colon;&num;04603e" class&equals;"has-inline-color"><strong>only within the same package<&sol;strong>&period;<&sol;span><&sol;p>&NewLine;&NewLine;&NewLine;&NewLine;<h5 class&equals;"wp-block-heading"><strong><span style&equals;"color&colon;&num;560399" class&equals;"has-inline-color">Example<&sol;span><&sol;strong><&sol;h5>&NewLine;&NewLine;&NewLine;&NewLine;<p>In this example we have two classes&comma; TestC class is trying to access the default members of TestA class&comma; since class TestC belongs to a different package&comma; this program would throw compilation error&comma; because the scope of default modifier is limited to the same package in which it is declared&period;<&sol;p>&NewLine;&NewLine;&NewLine;&NewLine;<p><strong><span style&equals;"color&colon;&num;0000ff" class&equals;"has-inline-color">TestA&period;java<&sol;span><&sol;strong><&sol;p>&NewLine;&NewLine;&NewLine;&NewLine;<pre class&equals;"wp-block-preformatted"><strong>package <span style&equals;"color&colon;&num;560399" class&equals;"has-inline-color">com&period;c1ctech&period;pkg1<&sol;span><&sol;strong>&semi;&NewLine;&NewLine;<em><span style&equals;"color&colon;&num;04603e" class&equals;"has-inline-color"><strong>&sol;&sol;public class<&sol;strong><&sol;span>&NewLine;<&sol;em><strong>public class <&sol;strong>TestA &lbrace;&NewLine;&NewLine; <em><strong><span style&equals;"color&colon;&num;04603e" class&equals;"has-inline-color">&sol;&sol;default data member<&sol;span><&sol;strong>&NewLine; <&sol;em><strong>int a<&sol;strong>&semi;&NewLine;&NewLine; <em><strong><span style&equals;"color&colon;&num;04603e" class&equals;"has-inline-color">&sol;&sol;default method<&sol;span><&sol;strong>&NewLine; <&sol;em><strong>void <&sol;strong>defaultMethod&lpar;&rpar; &lbrace;&NewLine; System&period;<strong><em>out<&sol;em><&sol;strong>&period;println&lpar;<strong>"defaultMethod of class TestA"<&sol;strong>&rpar;&semi;&NewLine; &rcub;&NewLine;&rcub;<&sol;pre>&NewLine;&NewLine;&NewLine;&NewLine;<p><strong><span style&equals;"color&colon;&num;0000ff" class&equals;"has-inline-color">TestC&period;java<&sol;span><&sol;strong><&sol;p>&NewLine;&NewLine;&NewLine;&NewLine;<pre class&equals;"wp-block-preformatted"><strong>package <span style&equals;"color&colon;&num;560399" class&equals;"has-inline-color">com&period;c1ctech&period;pkg2<&sol;span><&sol;strong>&semi;&NewLine;&NewLine;<strong>import <&sol;strong>com&period;c1ctech&period;pkg1&period;TestA&semi;&NewLine;&NewLine;<em><strong><span style&equals;"color&colon;&num;04603e" class&equals;"has-inline-color">&sol;&sol;default class<&sol;span><&sol;strong>&NewLine;<&sol;em><strong>class <&sol;strong>TestC &lbrace;&NewLine;&NewLine; <strong>public static void <&sol;strong>main&lpar;String&lbrack;&rsqb; args&rpar; &lbrace;&NewLine;&NewLine; <em><strong><span style&equals;"color&colon;&num;04603e" class&equals;"has-inline-color">&sol;&sol;error&colon; cannot access default members outside the package<&sol;span><&sol;strong>&NewLine; <&sol;em><strong>new <&sol;strong>TestA&lpar;&rpar;&period;<strong>a <&sol;strong>&equals; 10&semi;&NewLine; <strong>new <&sol;strong>TestA&lpar;&rpar;&period;defaultMethod&lpar;&rpar;&semi;&NewLine;&NewLine; &rcub;&NewLine;&rcub;<&sol;pre>&NewLine;&NewLine;&NewLine;&NewLine;<h3 class&equals;"wp-block-heading"><strong><span style&equals;"color&colon;&num;520599" class&equals;"has-inline-color">Private Access Modifier<&sol;span><&sol;strong><&sol;h3>&NewLine;&NewLine;&NewLine;&NewLine;<p>The scope of private modifier is limited to the class only&period;<&sol;p>&NewLine;&NewLine;&NewLine;&NewLine;<ul class&equals;"wp-block-list"><li>Private Data members and methods are only accessible within the class&period;<&sol;li><&sol;ul>&NewLine;&NewLine;&NewLine;&NewLine;<ul class&equals;"wp-block-list"><li><strong><span style&equals;"color&colon;&num;04603e" class&equals;"has-inline-color">Class<&sol;span><&sol;strong> and&nbsp&semi;<strong><span style&equals;"color&colon;&num;04603e" class&equals;"has-inline-color">Interface<&sol;span><&sol;strong> cannot be declared as <strong><span style&equals;"color&colon;&num;04603e" class&equals;"has-inline-color">private<&sol;span><&sol;strong>&period;<&sol;li><&sol;ul>&NewLine;&NewLine;&NewLine;&NewLine;<h5 class&equals;"wp-block-heading"><strong><span style&equals;"color&colon;&num;520599" class&equals;"has-inline-color">Example<&sol;span><&sol;strong><&sol;h5>&NewLine;&NewLine;&NewLine;&NewLine;<p>This example throws compilation error because we are trying to access the private data member and method of class TestA in class TestB&period; The private data member and method are only accessible within the class&period;<&sol;p>&NewLine;&NewLine;&NewLine;&NewLine;<p><strong><span style&equals;"color&colon;&num;0000ff" class&equals;"has-inline-color">TestA&period;java<&sol;span><&sol;strong><&sol;p>&NewLine;&NewLine;&NewLine;&NewLine;<pre class&equals;"wp-block-preformatted"><strong>package <&sol;strong>com&period;c1ctech&period;pkg1&semi;&NewLine;&NewLine;<em><strong><span style&equals;"color&colon;&num;04603e" class&equals;"has-inline-color">&sol;&sol;public class<&sol;span><&sol;strong>&NewLine;<&sol;em><strong>public class <&sol;strong>TestA &lbrace;&NewLine;&NewLine; <em><strong><span style&equals;"color&colon;&num;04603e" class&equals;"has-inline-color">&sol;&sol;private data member<&sol;span><&sol;strong>&NewLine; <&sol;em><strong>private int a<&sol;strong>&semi;&NewLine;&NewLine; <em><strong><span style&equals;"color&colon;&num;04603e" class&equals;"has-inline-color">&sol;&sol;private method<&sol;span><&sol;strong>&NewLine; <&sol;em><strong>private void <&sol;strong>privateMethod&lpar;&rpar; &lbrace;&NewLine; System&period;<strong><em>out<&sol;em><&sol;strong>&period;println&lpar;<strong>"privateMethod of class TestA"<&sol;strong>&rpar;&semi;&NewLine; &rcub;&NewLine;&NewLine; <strong>void <&sol;strong>defaultMethod&lpar;&rpar; &lbrace;&NewLine; System&period;<strong><em>out<&sol;em><&sol;strong>&period;println&lpar;<strong>"defaultMethod of class TestA"<&sol;strong>&rpar;&semi;&NewLine; <em><strong><span style&equals;"color&colon;&num;04603e" class&equals;"has-inline-color">&sol;&sol;access private method within class<&sol;span><&sol;strong>&NewLine; <&sol;em>privateMethod&lpar;&rpar;&semi;&NewLine; &rcub;&NewLine;&rcub;<&sol;pre>&NewLine;&NewLine;&NewLine;&NewLine;<p><strong><span style&equals;"color&colon;&num;0000ff" class&equals;"has-inline-color">TestB&period;java<&sol;span><&sol;strong><&sol;p>&NewLine;<&excl;-- WP QUADS Content Ad Plugin v&period; 2&period;0&period;98&period;1 -->&NewLine;<div class&equals;"quads-location quads-ad2" id&equals;"quads-ad2" style&equals;"float&colon;none&semi;margin&colon;0px&semi;">&NewLine;&NewLine;<&sol;div>&NewLine;&NewLine;&NewLine;&NewLine;&NewLine;<pre class&equals;"wp-block-preformatted"><strong>package <&sol;strong>com&period;c1ctech&period;pkg1&semi;&NewLine;&NewLine;<strong>class <&sol;strong>TestB &lbrace;&NewLine;&NewLine; <strong>public static void <&sol;strong>main&lpar;String&lbrack;&rsqb; args&rpar; &lbrace;&NewLine;&NewLine; <em><strong><span style&equals;"color&colon;&num;04603e" class&equals;"has-inline-color">&sol;&sol;error&colon; cannot access private members outside the class TestA<&sol;span><&sol;strong>&NewLine; <&sol;em><strong>new <&sol;strong>TestA&lpar;&rpar;&period;<strong>a <&sol;strong>&equals; 10&semi;&NewLine; <strong>new <&sol;strong>TestA&lpar;&rpar;&period;privateMethod&lpar;&rpar;&semi;&NewLine; &rcub;&NewLine;&NewLine;&rcub;<&sol;pre>&NewLine;&NewLine;&NewLine;&NewLine;<h3 class&equals;"wp-block-heading"><strong><span style&equals;"color&colon;&num;520599" class&equals;"has-inline-color">Protected Access Modifier<&sol;span><&sol;strong><&sol;h3>&NewLine;&NewLine;&NewLine;&NewLine;<p>Protected data member and method are only accessible by the classes of the same package and the subclasses present in any package&period; <&sol;p>&NewLine;&NewLine;&NewLine;&NewLine;<p>Protected access modifier is similar to default access modifier with one exception that it has visibility in sub classes&period;<&sol;p>&NewLine;&NewLine;&NewLine;&NewLine;<p>Classes cannot be declared as <strong><span style&equals;"color&colon;&num;04603e" class&equals;"has-inline-color">protected<&sol;span><&sol;strong>&period;&nbsp&semi;<&sol;p>&NewLine;&NewLine;&NewLine;&NewLine;<h5 class&equals;"wp-block-heading"><strong><span style&equals;"color&colon;&num;560399" class&equals;"has-inline-color">Example<&sol;span><&sol;strong><&sol;h5>&NewLine;&NewLine;&NewLine;&NewLine;<p>In this example the class <strong>TestC<&sol;strong> which is present in another package is able to access the&nbsp&semi;protected members of class <strong>TestA<&sol;strong>&period; This is because the TestC class extends class TestA and the protected modifier allows the access of protected members in subclasses &lpar;in any packages&rpar;&period;<&sol;p>&NewLine;&NewLine;&NewLine;&NewLine;<p><strong><span style&equals;"color&colon;&num;0000ff" class&equals;"has-inline-color">TestA&period;java<&sol;span><&sol;strong><&sol;p>&NewLine;&NewLine;&NewLine;&NewLine;<pre class&equals;"wp-block-preformatted"><strong>package <span style&equals;"color&colon;&num;520599" class&equals;"has-inline-color">com&period;c1ctech&period;pkg1<&sol;span><&sol;strong>&semi;&NewLine;&NewLine;<em><strong><span style&equals;"color&colon;&num;04603e" class&equals;"has-inline-color">&sol;&sol;public class<&sol;span><&sol;strong>&NewLine;<&sol;em><strong>public class <&sol;strong>TestA &lbrace;&NewLine;&NewLine; <em><strong><span style&equals;"color&colon;&num;04603e" class&equals;"has-inline-color">&sol;&sol;protected data member<&sol;span><&sol;strong>&NewLine; <&sol;em><strong>protected int a<&sol;strong>&semi;&NewLine;&NewLine; <em><strong><span style&equals;"color&colon;&num;04603e" class&equals;"has-inline-color">&sol;&sol;protected method<&sol;span><&sol;strong>&NewLine; <&sol;em><strong>protected void <&sol;strong>protectedMethod&lpar;&rpar; &lbrace;&NewLine; System&period;<strong><em>out<&sol;em><&sol;strong>&period;println&lpar;<strong>"protectedMethod of class TestA"<&sol;strong>&rpar;&semi;&NewLine; &rcub;<em>&NewLine;<&sol;em>&rcub;<&sol;pre>&NewLine;&NewLine;&NewLine;&NewLine;<p><strong><span style&equals;"color&colon;&num;0000ff" class&equals;"has-inline-color">TestC&period;java<&sol;span><&sol;strong><&sol;p>&NewLine;&NewLine;&NewLine;&NewLine;<pre class&equals;"wp-block-preformatted"><strong>package <span style&equals;"color&colon;&num;520599" class&equals;"has-inline-color">com&period;c1ctech&period;pkg2<&sol;span><&sol;strong>&semi;&NewLine;&NewLine;<strong>import <&sol;strong>com&period;c1ctech&period;pkg1&period;TestA&semi;&NewLine;&NewLine;<em><strong><span style&equals;"color&colon;&num;04603e" class&equals;"has-inline-color">&sol;&sol;default class<&sol;span><&sol;strong>&NewLine;<&sol;em><strong>class <&sol;strong>TestC <strong>extends <&sol;strong>TestA &lbrace;&NewLine;&NewLine; <strong>public static void <&sol;strong>main&lpar;String&lbrack;&rsqb; args&rpar; &lbrace;&NewLine;&NewLine; <em><strong><span style&equals;"color&colon;&num;04603e" class&equals;"has-inline-color">&sol;&sol; compile time error<&sol;span><&sol;strong>&NewLine; <&sol;em><strong>&sol;&sol;new <&sol;strong>TestA&lpar;&rpar;&period;<strong>a <&sol;strong>&equals; 10&semi;&NewLine; &sol;&sol;<strong>new <&sol;strong>TestA&lpar;&rpar;&period;protectedMethod&lpar;&rpar;&semi;&NewLine;&NewLine; <em><strong><span style&equals;"color&colon;&num;04603e" class&equals;"has-inline-color">&sol;&sol; works&comma; accessing super class protected members using subclass<&sol;span><&sol;strong>&NewLine; <&sol;em><strong>new <&sol;strong>TestC&lpar;&rpar;&period;<strong>a <&sol;strong>&equals; 10&semi;&NewLine; <strong>new <&sol;strong>TestC&lpar;&rpar;&period;protectedMethod&lpar;&rpar;&semi;&NewLine; &rcub;&NewLine;&rcub;<&sol;pre>&NewLine;&NewLine;&NewLine;&NewLine;<h3 class&equals;"wp-block-heading"><strong><span style&equals;"color&colon;&num;520599" class&equals;"has-inline-color">Public Access Modifier<&sol;span><&sol;strong><&sol;h3>&NewLine;&NewLine;&NewLine;&NewLine;<p>The data members&comma; methods and classes that are declared <strong><span style&equals;"color&colon;&num;04603e" class&equals;"has-inline-color">public<&sol;span><&sol;strong> can be accessed from anywhere&period; This modifier doesn’t put any restriction on the access&period;<&sol;p>&NewLine;&NewLine;&NewLine;&NewLine;<h5 class&equals;"wp-block-heading"><strong><span style&equals;"color&colon;&num;560399" class&equals;"has-inline-color">Example<&sol;span><&sol;strong><&sol;h5>&NewLine;&NewLine;&NewLine;&NewLine;<p>In this example we have two classes&comma; TestC class is trying to access the public members of TestA class &lpar;belongs to different package &rpar; and it works as public members are accessible everywhere&period;<&sol;p>&NewLine;&NewLine;&NewLine;&NewLine;<p><strong><span style&equals;"color&colon;&num;0000ff" class&equals;"has-inline-color">TestA&period;java<&sol;span><&sol;strong><&sol;p>&NewLine;&NewLine;&NewLine;&NewLine;<pre class&equals;"wp-block-preformatted"><strong>package <&sol;strong>com&period;c1ctech&period;pkg1&semi;&NewLine;&NewLine;<em><strong><span style&equals;"color&colon;&num;04603e" class&equals;"has-inline-color">&sol;&sol;public class<&sol;span><&sol;strong>&NewLine;<&sol;em><strong>public class <&sol;strong>TestA &lbrace;&NewLine;&NewLine; <em><strong><span style&equals;"color&colon;&num;04603e" class&equals;"has-inline-color">&sol;&sol;public data member<&sol;span><&sol;strong>&NewLine; <&sol;em><strong>public int a<&sol;strong>&semi;&NewLine;&NewLine; <em><strong><span style&equals;"color&colon;&num;04603e" class&equals;"has-inline-color">&sol;&sol;public method<&sol;span><&sol;strong>&NewLine; <&sol;em><strong>public void <&sol;strong>publicMethod&lpar;&rpar; &lbrace;&NewLine; System&period;<strong><em>out<&sol;em><&sol;strong>&period;println&lpar;<strong>"publicMethod of class TestA"<&sol;strong>&rpar;&semi;&NewLine; &rcub;&NewLine;&rcub;<&sol;pre>&NewLine;&NewLine;&NewLine;&NewLine;<p><strong><span style&equals;"color&colon;&num;0000ff" class&equals;"has-inline-color">TestC&period;java<&sol;span><&sol;strong><&sol;p>&NewLine;&NewLine;&NewLine;&NewLine;<pre class&equals;"wp-block-preformatted"><strong>package <&sol;strong>com&period;c1ctech&period;pkg2&semi;&NewLine;&NewLine;<strong>import <&sol;strong>com&period;c1ctech&period;pkg1&period;TestA&semi;&NewLine;&NewLine;<em><strong><span style&equals;"color&colon;&num;04603e" class&equals;"has-inline-color">&sol;&sol;default class<&sol;span><&sol;strong>&NewLine;<&sol;em><strong>class <&sol;strong>TestC &lbrace;&NewLine;&NewLine; <strong>public static void <&sol;strong>main&lpar;String&lbrack;&rsqb; args&rpar; &lbrace;&NewLine;&NewLine; <em><strong><span style&equals;"color&colon;&num;04603e" class&equals;"has-inline-color">&sol;&sol;works&comma; public members are accessible everywhere&period;<&sol;span><&sol;strong>&NewLine; <&sol;em><strong>new <&sol;strong>TestA&lpar;&rpar;&period;<strong>a <&sol;strong>&equals; 10&semi;&NewLine; <strong>new <&sol;strong>TestA&lpar;&rpar;&period;publicMethod&lpar;&rpar;&semi;<em>&NewLine; <&sol;em>&rcub;&NewLine;&rcub;<&sol;pre>&NewLine;&NewLine;

Exit mobile version