» » Accessibility modifiers examples


  • The keywords which define accessibility permissions are called accessibility modifiers.
  • Java supports four accessibility modifiers to define accessibility permissions at different levels.

Accessibility modifier keywords: 

 1.private

 2.protected

 3.public

 4.default(no keyword)

1.private:

  • The class members which have private keyword in its creation statement are called private members. Those members are only accessible within that class.
  • If we declare any variable or method with private accessibility modifier then those variables and methods are accessible only within that class , not accessible out side the class.

private variable are accessible within the class :


  1. package com.instanceofjava;
  2.  
  3. public class PrivateDemo {
  4.  
  5.     private String first_name;
  6.     private String last_name;
  7.  
  8. void show(){
  9.  
  10.   System.out.println("First Name:="+first_name);
  11.   System.out.println("Last Name:="+last_name);
  12.  
  13. }
  14.  
  15. public static void main(String[] args) {
  16.  
  17.        PrivateDemo obj= new PrivateDemo ();
  18.  
  19.         obj.first_name="James";
  20.         obj.last_name="Goosling";
  21.         obj.show();
  22.  
  23.     }
  24. }

Output:

  1. First Name:=James
  2. Last Name:=Goosling

private variable are not  accessible out side the class :

  1. package com.instanceofjava;
  2.  
  3. public class PrivateDemo {
  4.  
  5.     private String first_name;
  6.     private String last_name;
  7.  
  8. void show(){
  9.  
  10.   System.out.println("First Name:="+first_name);
  11.   System.out.println("Last Name:="+last_name);
  12.  
  13. }
  14.  
  15. public static void main(String[] args) {
  16.  
  17.        PrivateDemo obj= new PrivateDemo ();
  18.  
  19.         obj.first_name="James";
  20.         obj.last_name="Goosling";
  21.       
  22.  
  23.     }
  24. }

  1. package instanceofjava;
  2.  
  3. class Demo {
  4.  
  5. public static void main(String[] args){
  6.  
  7. PrivateDemo obj= new PrivateDemo ();
  8.  
  9.         obj.first_name="James"; // ERROR: The field PrivateDemo.first_name is not visible
  10.  }
  11.  
  12. }

private methods are accessible within the class :

  1. package com.instanceofjava;
  2.  
  3. public class PrivateDemo {
  4.  
  5.     private String first_name;
  6.     private String last_name;
  7.  
  8. private void show(){
  9.  
  10.   System.out.println("First Name:="+first_name);
  11.   System.out.println("Last Name:="+last_name);
  12.  
  13. }
  14.  
  15. public static void main(String[] args) {
  16.  
  17.        PrivateDemo obj= new PrivateDemo ();
  18.  
  19.         obj.first_name="James";
  20.         obj.last_name="Goosling";
  21.         obj.show();
  22.  
  23.     }
  24. }

Output:

  1. First Name:=James
  2. Last Name:=Goosling

private variable are not  accessible out side the class :

  1. package com.instanceofjava;
  2.  
  3. public class PrivateDemo {
  4.  
  5.     private String first_name;
  6.     private String last_name;
  7.  
  8. void show(){
  9.  
  10.   System.out.println("First Name:="+first_name);
  11.   System.out.println("Last Name:="+last_name);
  12.  
  13. }
  14.  
  15. public static void main(String[] args) {
  16.  
  17.        PrivateDemo obj= new PrivateDemo ();
  18.  
  19.         obj.first_name="James";
  20.         obj.last_name="Goosling";
  21.       
  22.  
  23.     }
  24. }

  1. package instanceofjava;
  2.  
  3. class Demo {
  4.  
  5. public static void main(String[] args){
  6.  
  7. PrivateDemo obj= new PrivateDemo ();
  8.  
  9.         obj.add(); // ERROR: The method add() from the type PrivateDemo is not visible
  10.  }
  11.  
  12. }



  • private variables and methods are accessible inside that class only. If we declare any variable or method as private , not accessible outside the class.

2.protected

  • The class members which have protected keyword in its creation statements are called protected members. Those members can be accessible with in package from all classes, but from out side package only in subclass that too using subclass name or its object.
  • protected variables accessible inside the package anywhere. Outside package accessible only in sub classes.

Same package anywhere:

  1. package com.instanceofjava;
  2.  
  3. public class ProtectedDemo {
  4.  
  5.     protected int a;
  6.     protected int b;
  7.  
  8. protected void show(){
  9.  
  10.   System.out.println("a="+a);
  11.   System.out.println("b="+b);
  12.  
  13. }
  14.  
  15. public static void main(String[] args) {
  16.  
  17.        ProtectedDemo obj= new ProtectedDemo ();
  18.  
  19.         obj.a=12;
  20.         obj.b=13;
  21.         obj.show();
  22.  
  23.     }
  24. }

Output:

  1. a=12
  2. b=13

Different package subclass:

  1. package com.accesiblitymodifiers;
  2.  
  3. public class Sample extends ProtectedDemo {
  4.  
  5. public static void main(String[] args) {
  6.  
  7.        Sample  obj= new Sample();
  8.  
  9.         obj.a=12;
  10.         obj.b=13;
  11.         obj.show();
  12.  
  13.     }
  14. }

Output:

  1. a=12
  2. b=13

3.public

  • If we declare any variable or method with public access specifier then those members will be accessible to everywhere.


  1. package com.instanceofjava;
  2.  
  3. public class PublicDemo {
  4.  
  5.     public int x;
  6.     public int y;
  7.  
  8. public void show(){
  9.  
  10.   System.out.println("x="+x);
  11.   System.out.println("y="+y);
  12.  
  13. }
  14.  
  15. public static void main(String[] args) {
  16.  
  17.        PublicDemo obj= new PublicDemo ();
  18.  
  19.         obj.x=1;
  20.         obj.y=2;
  21.         obj.show();
  22.  
  23.     }
  24. }


Output:

  1. x=1
  2. y=2

4.default

  • If we declare any member with no keyword those members are called default members.
  • default members are accessible to package level.
  • Means we can access anywhere in same package but we can not access in out side the package under any condition.
  • So default will acts as public inside package and private out side the package.

Instance Of Java

We will help you in learning.Please leave your comments and suggestions in comment section. if you any doubts please use search box provided right side. Search there for answers thank you.
«
Next
Newer Post
»
Previous
Older Post

No comments

Leave a Reply

Select Menu