Access Modifiers In Java

Access modifier defines the access level for classes, methods, variables and constructors. There are four access modifier used in java.

  • Public access modifier.
  • Protected access modifier.
  • Default(without keyword) access modifier.
  • Private access modifier.

Access Modifiers
Same Class
Same Package
Sub class
Other packages
public
yes
yes
yes
yes
protected
yes
yes
yes
no
default(without keyword)
yes
yes
no
no
private
yes
no
no
no


public Access Modifier

Classes, variables, methods, constructors declared public within a class are visible or accessible to any class, weather class is in same or in other package.

Syntax for declaring public access modifier

For class - public class class-name{}
For method - public return-type method-name(){}
For variable - public datatype variable-name;
For constructors - public constructor-name(){}


public Access Modifier Example
package A;
public class A
{
	public void show()
	{
		System.out.println("inside A class show()");
	}
}
package B;
import A.*;
class B
{
	public static void main(String s1[])
	{
		A a = new A();
		a.show();
	}
}
Output
inside A class show()


protected Access Modifier

Protected access modifier cannot be applied on classes as classes should be public or default. Protected variables and methods allow the class itself to access them, classes inside of the same package to access them, and subclasses of that class to access them.

Syntax for declaring protected access modifier

For method - protected return-type method-name(){}
For variable - protected datatype variable-name;
For constructors - protected constructor-name(){}

protected Access Modifier Example
package A;
public class A
{
	protected void show()
	{
		System.out.println("inside A class show()");
	}
}
package B;
import A.*;
class B extends A
{
	public static void main(String s1[])
	{
		B b = new B();
		b.show();
	}
} 
Output
inside A class show()

Default(without keyword) Access Modifier

Java use default access modifier if you don't mention any access modifier. Default means accessible within same package.

Syntax for declaring default access modifier

For class - class class-name{}
For method - return-type method-name(){}
For variable - datatype variable-name;
For constructors - constructor-name(){}

default(without keyword) Access Modifier Example
package A;
class A
{
	void show()
	{
		System.out.println("inside A class show()");
	}
}
			
package B;
import A.*;
class B
{
	public static void main(String s1[])
	{
		A a = new A();	//compile time error
		a.show();	//compile time error
	}
}
Output
Compile time error

Private Access Modifier

Private access modifier is accessible only by its class. Class cannot be a private as class should be public or default.

Syntax for declaring private access modifier

For method - private return-type method-name(){}
For variable - private datatype variable-name;
For constructors - private constructor-name(){}

private Access Modifier Example
package A;
class A
{
	private void show()
	{
		System.out.println("inside A class show()");
	}
}
package B;
import A.*;
class B
{
	public static void main(String s1[])
	{
		A a = new A();	
		a.show();	//compile time error
	}
}
Output
Compile time error