Java – Access Modifiers

Java Access Modifiers

We use access modifiers to set the access level for variables, methods, classes and constructors. We would examine the following:

  1. Default modifier (no keyword)
  2. Private Modifier (private keyword)
  3. Public Modifier (public keyword)
  4. Protected Modifier (protected keyword)
  5. About Inheritance

 

1. Default Modifier (No modifier keyword)

Here you don’t specify and modifier keyword. As such, the following rule applies:

Variable or Method declared without modifier is accessible to classes in the same package.

Variables in an interface are public by default.

 

2. Private Access Modifier (private)

Variables and methods declared are accessible  online within the class. That is only by method in the same class. You specify private access modifier using the private keyword. This modifier provides the highest level of restriction.

The only way to access private variables is to provide public getter and setter methods in the class.

Private keyword cannot be applied to classes and interfaces.

public class Building {

	private int numberOfRooms; //private variable
	private String address;    //private variable
	
	public int getNumberOfRooms() { //public getter
		return numberOfRooms;
	}

	public String getAddress() {    //public getter
		return address;
	}

	@Override
	public String toString() {
		return "Building [numberOfRooms=" + numberOfRooms + ", address=" + address + "]";
	}	
}

Listing 1.0: Class with private variables and public getters

 

The variables NumberOfRooms and Address are both private. But the getNumberOfRooms and getAddress methods are both public. We use this getters to make private variables available outside the class.

 

3. Public Access Modifier (public)

Almost anything can be declared as public: classes, interfaces, methods, variables etc. As such, these can be accessible from outside the class.

If however the class is in another package, then you need to import the package to use the class. Public methods and variables of a class are inherited by the subclass.

An example of a public class  is in Listing 1.0 above. The class Building is declared as public.

 

4.Protected Access Modifier (protected)

Protected can be applied to variables, methods and constructors. However, you cannot declare a class or interface as protected. Protected variables or methods are accessible only by subclasses. This includes subclasses both within and outside the package.

Moreover,  methods and fields in an interface cannot be protected. This is because, classes that implements interface automatically have access to these methods/variables. And the only way to use an interface is to implement it.

An example is given in Listing 1.1

public class Shape {
	protected int sides;
	protected String typeOfShape;
	
	protected void render() {
		
	}	
}

Listing 1.1: Protected Access Modifier

I recommend you run this code yourself. Also try to change the modifiers and see the result. The variables sides and typeOfShape are both protected.

 

5. About Inheritance

When you create and inherited class, then the following rules apply:

  • Private methods in superclass are not inherited by subclasses
  • Protected methods in parent class must be public or protected in child class
  • Public method in superclass must also be public in subclasses

Finally, note that superclass is same as parent class. Similarly, subclass is same as child class.