Java – Access Modifiers

Java provides four access modifiers to set access levels for classes, variables, methods and constructors i.e. public, private, protected and default. These access level modifiers determine whether other classes can use a particular field or invoke a particular method.

1. Java access modifiers

Let’s quickly compare these access modifiers in nutshell.

  1. public – accessible everywhere
  2. protected – accessible in the same package and in sub-classes
  3. default – accessible only in the same package
  4. private – accessible only in the same class

The access specifiers can be strictly ordered as below :

public > protected > package-private (or default) > private

1.1. public

Public are accessible from everywhere. A class, method, constructor, interface, etc declared public can be accessed from any other class.

However, if the public class we are trying to access is in a different package, then the public class still needs to be imported.

public class HelloWorld 
{
   private String format;

   public String getFormat() {
      return this.format;
   }
   public void setFormat(String format) {
      this.format = format;
   }
}

In above example, getFormat() and setFormat() methods are public, so they can be accessed anywhere.

1.2. protected

Protected are accessible by the classes of the same package and the subclasses residing in any package. Protected access gives the subclass a chance to use the helper method or variable while preventing a non-related class from trying to use it.

public class HelloWorld 
{
   private String format;

   protected String getFormat() {
      return this.format;
   }
   protected void setFormat(String format) {
      this.format = format;
   }
}

In above given example of HelloWorld, variable format is declared protected, so it can be accessed by all the classes present in same package where HelloWorld.java is present, as well as sub-classes present in other packages as well.

1.3. default (package private)

Default access modifier means we do not explicitly declare an access modifier for a class, field, method, etc. Default are accessible by the classes of the same package.

Please note that the fields in an interface are implicitly public static final and the methods in an interface are by default public.

public class HelloWorld 
{
   String format;

   public String getFormat() {
      return this.format;
   }
   public void setFormat(String format) {
      this.format = format;
   }
}

In above given example of HelloWorld, variable format is declared default, so it can be accessed by all the classes present in same package where HelloWorld.java is present.

1.4. private

A private access modifier is the most restrictive access level. (Topmost) Classes and interfaces cannot be private. private members are accessible within the same class only. Methods, Variables, and Constructors that are declared private can only be accessed within the declared class itself.

In above given example of HelloWorld, variable format is declared private, so no class can access it directly. It must be accessed though public methods getFormat() and setFormat().

Access levels affect you in two ways. First, when you use classes that come from another source, access levels determine which members of those classes your own classes can use. Second, when you write a class, you need to decide what access level every member variable and every method in your class should have.

Local variables and formal parameters cannot take access specifiers. Since they are inherently inaccessible to the outside according to scoping rules, they are effectively private.

If other programmers use your class, you want to ensure that errors from misuse cannot happen. Access levels can help you do this.

2. Levels of Access Control

There are two levels of access control.

  1. Class level — Allowed modifiers are public, or package-private (default).
  2. Method level — Allowed modifiers are public, private, protected, or package-private (default).

A class may be declared with the modifier public, in which case that class is visible to all classes everywhere. If a class has no modifier (the default, also known as package-private), it is visible only within its own package.

For members, there are two additional access modifiers: private and protected. The private modifier specifies that the member can only be accessed in its own class.

The protected modifier specifies that the member can only be accessed within its own package (as with package-private) and, in addition, by a subclass of its class in another package.

Both private and protected can be (and frequently are) applied to nested classes and interfaces, just never top-level classes and interfaces.

Happy Learning !!

Ref: https://docs.oracle.com/javase/tutorial/java/javaOO/accesscontrol.html

Was this post helpful?

Join 7000+ Fellow Programmers

Subscribe to get new post notifications, industry updates, best practices, and much more. Directly into your inbox, for free.

2 thoughts on “Java – Access Modifiers”

Comments are closed.

HowToDoInJava

A blog about Java and its related technologies, the best practices, algorithms, interview questions, scripting languages, and Python.