Describing Access Modifiers in Java

What are Access Modifiers in Java?

 
In Java, the access modifiers give accessibility (scope) of a data member, method, constructor or class. Access modifiers help us to set the level of access for our class, variables, and methods. In Java, two types of modifiers are access modifiers and non-access modifiers. In Java, Access modifiers  can be defined as

Keywords that help set the visibility and accessibility of a class, its member variables, and methods are called Access Modifier.

Access modifiers specify who can access them. There are four access modifiers used in java. They are public, private, protected, default  (declaring without an access modifier). Using no modifier is also sometimes referred to as "package-private" or "default" or "friendly" access.

Usage of these access modifiers is restricted to two levels. The two levels are class-level access modifiers and member level access modifiers:

Class level access modifiers

 
Only two access modifiers are allowed, public and no modifier. If a class is 'public', then it can be accessed from anywhere. If a class has "no modifier", then it can only be accessed from "same" package'.
 

Member level access modifiers

 
All the four public, private, protected and no modifier is allowed. public and no modifier - the same way as used in class level. private - members can only access. protected - can be accessed from the same package and a subclass existing in any package can access.
 
The Following Table shows what Access Modifiers are appropriate for classes, nested classes, member variables, interface, and interface methods.  
 
 Access Modifier Class Nested class Member variable  Interface  Interface method
public visible from anywhere same as its class same as its class visible from anywhere visible from anywhere
protected N/A it's class and it's a subclass it is class and it's a subclass N/A N/A
package only from its a package only From its the package only from its package N/A N/A, the default package
default N/A only from its class only from its class N/A N/A

Public Keyword

 
The public is a Java keyword which declares a member's access as public. Public members are visible to all other classes. This means that any other class can access a public field or method. Further, other classes can modify public fields unless the field is declared as final. A best practice is to give fields private access and reserve public access to only the set of methods and final fields that define the class' public constants. This helps with encapsulation and information hiding since it allows you to change the implementation of a class without affecting the consumers who use only the public API of the class.
 

Protected Keyword

 
Protected is a Java keyword.  This keyword is an access modifier, used before a method or other class member to signify that the method or variable can only be accessed by elements residing in its class, subclasses, or classes in the same package.
 
Syntax:
 
protected <return Type> <method Name>(<parameters>);

For example:
 
protected int getAge();

protected void setYearOfBirth(int year);

Private Keyword

 
Private is a Java keyword which declares a member's access as private. That is, the member is only visible within the class, not from any class. The visibility of private members extends to nested classes.

Syntax:
 
private void method();
 

Default Access Modifier - No keyword

 
Default access modifier means we do not explicitly declare an access modifier for a class, field, method, etc. A variable or method declared without any access control modifier is available to any other class in the same package. The default modifier cannot be used for methods, fields in an interface.

Example:

Variables and methods can be declared without any modifiers, as in the following examples:

Some Example Of Uses Of these Access Specifier.
 
A.java:
  1. class Exp 
  2. {  
  3.     private String name;    
  4.     protected void set(String nm) 
  5.     {  
  6.         name = nm;  
  7.     }    
  8.     public Exp(String name) 
  9.     {  
  10.         this.name = name;  
  11.     }    
  12.     public String toString() 
  13.     {  
  14.         return "I'm a vikas and my name is " + name;  
  15.     }  
  16. }    
  17. public class A extends Exp 
  18. {  
  19.     private int aNumber;    
  20.     public A(String name, int aNumber)
  21.     {  
  22.         super(name);  
  23.         this.aNumber = aNumber;  
  24.     }    
  25.     public void change(String name, int aNumber) 
  26.     {  
  27.         set(name);  
  28.         this.aNumber = aNumber;  
  29.     }    
  30.     public String toString() 
  31.     {  
  32.         return "A" + aNumber + ": " + super.toString();  
  33.     }    
  34.     public static void main(String[] args) 
  35.     {  
  36.         A a = new A("vikky"12);  
  37.         System.out.println(a);  
  38.         a.change("mishra"19);  
  39.         System.out.println(a);  
  40.     }  
  41. }  
 
Output: A.java:
 
A.java.jpg
 
My1.java:
  1. class MySingleton 
  2. {  
  3.     private static MySingleton theObject;    
  4.     private MySingleton() {  
  5.     }    
  6.     public static MySingleton createMySingleton()
  7.     {  
  8.         if (theObject == null)  
  9.             theObject = new MySingleton();  
  10.         return theObject;  
  11.     }  
  12. }    
  13. public class My1 
  14. {  
  15.     public static void main(String[] args) 
  16.     {  
  17.         MySingleton ms1 = MySingleton.createMySingleton();  
  18.         MySingleton ms2 = MySingleton.createMySingleton();  
  19.         System.out.println(ms1 == ms2);  
  20.     }  
  21. }    
Output:
 
my1..jpg
 

Summary 

 
In this article, we learned about Access Modifiers in Java and how to use these access modifiers in Java Programs.