Access Modifiers in Java

The process of accessing a class, constructor, methods, and attributes are defined by Java Access Modifiers. We use the modifier to give accessibility to the method, class, variables and class members. A class can control what data or information should be exposed to other class with the help of access modifier.

Java provides a number of access modifiers to set the level of access for class, methods, constructor, and attributes. A member has the package or default accessibility if no access modifier is specified.

Types of Access Modifiers in Java

  • Private

  • Protected

  • Default

  • Public

Modifiers can be used as –

public class Color{ ….. }
private int number = 500;
protected String name = “JAVA”;
public String getcolor(){ …. }
public setColor(String colorName){ ….. }

1. Public Access Modifier in Java

the public modifier can be used for class, method, member variables, and constructors. This modifier is the least restrictive modifier. Public members are visible to any class in the java program, whether these classes are in the same package or another package.

Look at the below Example:

package academic;

public class School {

	public String schoolName;
	public String schoolAddress;
	public int schoolStaffCount;

	public School() {
		super();
	}


	/**
	 * @return the schoolName
	 */
	public String getSchoolName() {
		return schoolName;
	}

	/**
	 * @param schoolName the schoolName to set
	 */
	public void setSchoolName(String schoolName) {
		this.schoolName = schoolName;
	}

	/**
	 * @return the schoolAddress
	 */
	public String getSchoolAddress() {
		return schoolAddress;
	}

	/**
	 * @param schoolAddress the schoolAddress to set
	 */
	public void setSchoolAddress(String schoolAddress) {
		this.schoolAddress = schoolAddress;
	}

	/**
	 * @return the schoolStaffCount
	 */
	public int getSchoolStaffCount() {
		return schoolStaffCount;
	}

	/**
	 * @param schoolStaffCount the schoolStaffCount to set
	 */
	public void setSchoolStaffCount(int schoolStaffCount) {
		this.schoolStaffCount = schoolStaffCount;
	}

}

Here we have created a School class where all the methods and variables and constructor are public. So it can be accessed from anywhere in a different package.

package test;

import academic.School;

public class TestSchool {

	public static void main(String[] args) {

		School school = new School();
		school.setSchoolName("Adarsh High School");
		school.setSchoolAddress("Andheri, Mumbai");
		school.setSchoolStaffCount(20);

		System.out.println("Schoool Name - " + school.schoolName); //statement 1
		System.out.println("School Address - " + school.schoolAddress); //statement 2
		System.out.println("School Staff Count - " + school.schoolStaffCount); //statement 3

	}
}
Example Output

Here you can see in statement 1, 2 and 3, we are able to access variable name outside the class by its object.

2. Private Access Modifier in Java

the private modifier can be used for class, method, member variables, and constructors. This modifier is the most restrictive modifier. Private attributes, methods cannot be accessed outside the class. Attributes, methods, constructor which are declared as private are strictly controlled. Which means they can not be accessed by anywhere outside the enclosing class.

A standard design strategy is to defining all fields of class as private and providing getters and setters methods for the fields to access them.

Look at the below program:

package academic;

public class School {

	private String schoolName;
	private String schoolAddress;
	private int schoolStaffCount;

	public School() {
		super();
	}

	/**
	 * @return the schoolName
	 */
	public String getSchoolName() {
		return schoolName;
	}

	/**
	 * @param schoolName the schoolName to set
	 */
	public void setSchoolName(String schoolName) {
		this.schoolName = schoolName;
	}

	/**
	 * @return the schoolAddress
	 */
	public String getSchoolAddress() {
		return schoolAddress;
	}

	/**
	 * @param schoolAddress the schoolAddress to set
	 */
	public void setSchoolAddress(String schoolAddress) {
		this.schoolAddress = schoolAddress;
	}

	/**
	 * @return the schoolStaffCount
	 */
	public int getSchoolStaffCount() {
		return schoolStaffCount;
	}

	/**
	 * @param schoolStaffCount the schoolStaffCount to set
	 */
	public void setSchoolStaffCount(int schoolStaffCount) {
		this.schoolStaffCount = schoolStaffCount;
	}
}

Now we are creating a test class to access this class in a different package.

package test;

import academic.School;

public class TestSchool {

	public static void main(String[] args) {

		School school = new School();
		school.setSchoolName("Adarsh High School");
		school.setSchoolAddress("Andheri, Mumbai");
		school.setSchoolStaffCount(20);

		System.out.println("Schoool Name - " + school.schoolName); //statement 1
		System.out.println("Schoool Address - " + school.schoolAddress); //statement 2
		System.out.println("Schoool Staff Count - " + school.schoolStaffCount); //statement 3
		
		System.out.println("Schoool Name - " + school.getSchoolName()); //statement 4
		System.out.println("Schoool Address - " + school.getSchoolAddress()); //statement 5
		System.out.println("Schoool Staff Count - " + school.getSchoolStaffCount()); //statement 6 
		

	}
}

Explanation:

Here statement 1, 2, 3 will give compile time error because fields in School class are private, hence direct access is not allowed outside the class. So compile-time error will be thrown.

Statement 4, 5, 6 will execute successfully because we are calling values of fields by getter methods. So when commenting out the statement 1, 2 and 3, the output will be-

Output:

School Name - Adarsh High School
School Address - Andheri, Mumbai
School Staff Count - 20

3. Protected Access Modifier in Java

the protected modifier can be used for class, method, member variables, and constructors. This modifier has less restriction than the private modifier. Attributes, methods, and constructor declared as protected in a superclass can only be accessed by the subclass of other packages. Protected field, method or constructor can be accessed within the same package, no matter they are subclass or not of the protected member’s class.

//save by Demo.java  
package pack;  
public class Demo{  
protected void msg(){System.out.println("Hello Java");}  
}  

//save by Test.java  
package mypack;  
import pack.*;  
  
class Test extends Demo{  
  public static void main(String args[]){  
   Test obj = new Test();  
   obj.msg();  
  }  
}
Example Output

4. Default Access Modifier in Java

When no access modifier is specified then it is assumed a default. In this case class field, method or constructor is accessible only by classes in the same package. In the case of Interface, the default modifier is not used for fields and methods.

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

Share