Access Modifiers in Java

Modifiers are applied to class, interface and its members. Java supports two types of modifiers-

  1. Access Modifier-
  2. Nonaccess Modifiers-
    • It controls the behaviour of class and its members.
    • Some of the nonaccess modifiers are- static, final, abstract, etc.

public access modifier

You can apply public access modifier to classes, interfaces, and their members. It makes Java element most accessible or least restrictive which means that element can be accessed from all packages.

Example of public access modifier

package package1;  
public class X
{  
	public void display()
	{
		System.out.println("I am in package1");
	}  
}
package package2;  
import package1.*;  
  
class Y
{
	public static void main(String args[])
	{  
   		X x = new X();  
   		x.display(); 
  	}
}

Output

I am in package1

protected access modifier

The members of a class defined with protected access modifiers are accessible in-

  • All classes and interfaces defined in the same package.
  • All derived classes, even they are in the separate package.

Example of protected access modifier

package package1;  
public class X
{  
	protected void display()
	{
		System.out.println("I am in package1");
	}  
}
package package2;
import package1.*;  
  
class Y
{
	public static void main(String args[])
	{  
   		X x = new X();  
   		x.display(); 
  	}
}

Output

Compile Time Error

Note- Members of an interface are implicitly public. If you declare interface members as protected, then they won't compile.

default access modifier

There is no keyword default for the default modifier. If an element does not explicitly use any modifier, the default access is implied. It may be applied to a class, a variable, or a method. It is only accessible within the package.

Example of default access modifier

package package1;  
class X
{  
	void display()
	{
		System.out.println("I am in package1");
	}  
}
package package2;
import package1.*;  
  
class Y
{
	public static void main(String args[])
	{  
   		X x = new X();  
   		x.display(); 
  	}
}

Output

Compile Time Error

private access modifier

  • It makes the Java element least accessible or most restrictive. You cannot apply private access modifier to top-level class and interface.
  • You can apply it to members of a top-level class (variables, methods, and inner classes).
  • A private member of a class can only be accessed from within the class.
  • In case, you declared private constructor then you cannot create instances of that class. Private members of a class never inherited.

Example of private access modifier

class X
{
	private int info = 10;
	private void display()
	{
		System.out.println("I am in X class");
	}
}
public class Y
{
	public static void main(String args[])
	{
		X x = new X();
		System.out.println(x.info);  
		x.display(); 
	}
}

Output

Compile Time Error

Overview all access modifiers in Java

Access Modifiers Same Package Different Package
Derived Classes Separate Classes Derived Classes Separate Classes
public Allowed Allowed Allowed Allowed
protected Allowed Allowed Allowed Not Allowed
default Allowed Allowed Not Allowed Not Allowed
private Not Allowed Not Allowed Not Allowed Not Allowed