Access Modifiers Keywords In java

In this article I am explaining Access Modifiers keywords in Java.

In access modifiers there are mainly the following three keywords:

  1. Private
  2. Protected
  3. Public

Here's the explanation:

Private:

The private keyword is an access modifier that can be applied to method, member variable and inner class.

  • Private Method:

    If a method marked as private, it cannot be invoked from outside the class it is declared. In other words, the private method is available to the enclosing class. For example:
    1. class A  
    2. {  
    3.     private void foo()  
    4.     {  
    5.     }  
    6.     void bar()  
    7.     {  
    8.         foo(); // okay, no problem!   
    9.     }  
    10. }  
    11. class B  
    12. {  
    13.     void woo()  
    14.     {  
    15.         A a = new A();  
    16.         a.foo(); // Oh no! since foo() is private   
    17.     }  
    18. }  
  • Private Variable:

    The private access modifier can also be applied to member variables which are declared within a class. Like private method, a private variable can be accessed only within its enclosing class. For example:
    1. class A  
    2. {  
    3.     private int number;  
    4.   
    5.     void bar()  
    6.     {  
    7.         number = 10; // OK   
    8.     }  
    9. }  
    10.   
    11. class B  
    12. {  
    13.     void foo()  
    14.     {  
    15.         A a = new A();  
    16.         a.number = 10. // Oh no! since number is private   
    17.     }  
    18. }  
  • Private inner class:

    An inner class is one that is declared inside another class. An inner class can be declared as private thus it can be accessed only from within the enclosing class, like private method and private variable. For example:
    1. class A {  
    2.     private class SubA {  
    3.         // inner class   
    4.     }  
    5.     void bar() {  
    6.         SubA obj = new SubA(); // OK   
    7.     }  
    8. }  
    9.   
    10. class B {  
    11.     void foo() {  
    12.         A.SubA a = new A.SubA(); // Oh no! since SubA is private   
    13.     }  
    14. }  

Protected:

The protected keyword is an access modifier for method and variable of a class. When a method or a variable is marked protected, it can be accessed from the following:

  • Within the enclosing class.
  • Other classes in the same package as the enclosing class.
  • Sub classes, regardless of packages.

The main purpose of protected keyword is to have the method or variable that can be inherited from sub classes.

Example:

The following class Person declares a protected variable name, inside package p1:

  1. package p1;  
  2.   
  3. public class Person {  
  4.     protected String name;  
  5. }  
The following class in the same package can access the variable name directly:
  1. package p1;  
  2.   
  3. public class Employer {  
  4.     void hireEmployee() {  
  5.         Person p = new Person();  
  6.         p.name = "Nam"// access protected variable directly   
  7.     }  
  8. }  
The following class is in different packages but it extends the Person class so it can access the variable name directly:
  1. package p2;  
  2.   
  3. import p1.Person;  
  4.   
  5. class Employee extends Person {  
  6.     void doStuff() {  
  7.         name = "Bob";  
  8.     }  
  9. }  
But the following class in different package cannot access the variable name directly:
  1. package p2;  
  2.   
  3. import p1.Person;  
  4.   
  5. class AnotherEmployer {  
  6.     void hire() {  
  7.         Person p = new Person();  
  8.         // compile error, cannot acceess protected variable   
  9.         // from different package   
  10.         p.name = "Nam";  
  11.     }  
  12. }  
Public:

The public keyword is an access modifier for class, method and variable:

 

  • When a class is marked public, it can be accessed from anywhere, including outside packages.
  • When a method is marked public, it can be invoked not only from the enclosing class, but also from outside classes.
  • When a variable is marked public, it can be accessed and updated from outside classes.

To summarize, public is the access modifier that has least restriction on the object it modifies.

The following code example shows a public class which has a public method eat() and a public variable name:

  1. public class Person {  
  2.     public String name;  
  3.     public void eat() {}  
  4. }  

 

Up Next
    Ebook Download
    View all
    Learn
    View all