Java Access Modifiers

In this tutorial, we will learn about what is an access modifier and types of access modifiers in Java, along with the syntax and guide lines to choose.

Access Modifiers

Access modifier is concept of limiting access of a class or its data members or methods associated of a class. By using this concept gives a greater flexibility for developer to limit the access of written code for others to avoid a misuse or interference.

The other definition of access modifier is specifying the scope of visibility for a class or its data members or methods associated of a class. Which are visible means those can be accessed and which are not visible cannot be accessed.

In java, Access modifiers are divided into four types based on the scope of visibility.

  1. public
  2. protected
  3. private
  4. default

public modifier

public access modifier is used to give the access globally, class or its members which are declared public can be accessed globally(can be accessed from anywhere of the application). To specify public scope, prefix a class or its members with the keyword public.

public modifier syntax:

public class class_name {}
public int variable_name;

protected modifier

protected access modifier is used to give the access for inherited classes only, class or its members which are declared protected can be accessed by its child classes only. To specify protected scope, prefix a class or its members with the keyword protected.

protected modifier syntax:

protected class class_name {}
protected int variable_name;

private modifier

private access modifier is used to give the access with in the specified class only, class or its members which are declared private can be accessed in that class only cannot be accessed outside of the class. In case of nested class, if inner class is declared as private then all its members can be accessed on outer class only. To specify private scope, prefix a class, or its members with the keyword private.

private modifier syntax:

private class class_name {}
private int variable_name;

default modifier

If we want to limit the access of a class or its members only under the current package default access modifier can be used. There is no need to specify any access modifier explicitly leaving blank will suffice.

default modifier syntax:

class class_name {}
int variable_name;

Modifiers access levels:

The following tabular form for easier understanding on modifiers.

Access ModifierClass levelPackage LevelChild ClassGlobally
publictruetruetruetrue
protectedtruetruetruefalse
default(no modifier)truetruefalsefalse
privatetruefalsefalsefalse

Guide lines of Access Modifiers:

  1. Try to avoid the global access otherwise it will restrict flexibility of manipulating the code.
  2. Always prefer to choose private until if there is strong reason not to.
  3. While writing frameworks consider using private to avoid misuse.
  4. Always good to choose specifier instead of leaving blank.

Conclusion:

In this tutorial, we have learned about what is an access modifier ,type of access modifiers in java along with syntax and guide lines to choose the specifier.