Which are four access modifiers in Java?

There are four different types of access modifiers in java - private, protected, public and default.

default - The access level of a default modifier is only within the package. It cannot be accessed from outside the package. If you do not specify any access level, it will be the default. This access is more restricted than public and protected but less restricted than private.

public - The access level of a public modifier is everywhere. It can be accessed from within the class, outside the class, within the package and outside the package. This is the simplest way to provide access to class members. However, we should take care of using this keyword with class variables otherwise anybody can change the values. Usually, class variables are kept as private and getter-setter methods are provided to work with them.

private - The access level of a private modifier is only within the class. It cannot be accessed from outside the class. Usually, we keep class variables as private and methods that are intended to be used only inside the class as private.

protected - The access level of a protected modifier is within the package and outside the package through child class. If you do not make the child class, it cannot be accessed from outside the package. Usually, we use this keyword to make sure the class variables are accessible only to the subclasses.

Table 1: Access Modifiers: Scope of Visibility
Access Modifier within class within package outside package by subclass only outside package
private YES NO NO NO
default YES YES NO NO
protected YES YES YES NO
public YES YES YES YES

Example of private access modifier: In this example, we have created two classes A and Simple. Class A contains private data member and private method. We are accessing these private members from outside the class, so there is a compile-time error.

class A {  
	private int data=40;  
	private void msg(){System.out.println("Hello java");}  
}
public class Simple{  
 public static void main(String args[]){  
   A obj=new A();  
   System.out.println(obj.data);//Compile Time Error  
   obj.msg();//Compile Time Error  
   }  
}

Role of Private Constructor: If you make any class constructor private, you cannot create the instance of that class from outside the class. For example:

class A{  
private A(){}//private constructor  
void msg(){System.out.println("Hello java");}  
}
public class Simple{  
 public static void main(String args[]){  
   A obj=new A();//Compile Time Error  
 }  
} 

Example of default access modifier: In this example, we have created two packages pack and mypack. We are accessing the A class from outside its package, since A class is not public, so it cannot be accessed from outside the package.

//save by A.java
package pack;  
class A{  
  void msg(){System.out.println("Hello");}  
}
//save by B.java
package mypack;  
import pack.*;  
class B{  
  public static void main(String args[]){  
   A obj = new A();//Compile Time Error  
   obj.msg();//Compile Time Error  
  }  
}

In the above example, the scope of class A and its method msg() is default so it cannot be accessed from outside the package.

Example of protected access modifier: In this example, we have created two packages - pack and mypack. Class A from pack is public, so it can be accessed from outside the package. But msg method of this package is declared as protected, so it can be accessed from outside the class only through inheritance.

//save by A.java  
package pack;  
public class A{  
	protected void msg(){System.out.println("Hello");}  
}
//save by B.java  
package mypack;  
import pack.*;  
 
class B extends A{  
  public static void main(String args[]){  
   B obj = new B();  
   obj.msg();  
  }  
}

Example of public access modifier:

//save by A.java  
package pack;  
public class A{  
	public void msg(){System.out.println("Hello");}  
}
//save by B.java  
package mypack;  
import pack.*;  
 
class B{  
  public static void main(String args[]){  
   A obj = new A();  
   obj.msg();  
  }  
}

Hope you have enjoyed reading this post. Please do write us if you have any suggestion/comment or come across any error on this page. Thank you for reading!



Share this page on WhatsApp

Get Free Tutorials by Email

About the Author

is the founder and main contributor for cs-fundamentals.com. He is a software professional (post graduated from BITS-Pilani) and loves writing technical articles on programming and data structures.