Access Modifiers in Java

access modifiers in Java helps to restrict the scope of a class, constructor , variable , method or data member. The four access levels are −

  • default (package-private) – also known as package access, the member can be accessed by any class within the same package
  • private – the member can only be accessed by other members within the same class
  • public – the member can be accessed from anywhere
  • protected – the member is only inaccessible from non-subclasses in a different package

Default Access Modifier – No Keyword

When we do not mention any access modifier, it is called default access modifier. The scope of this modifier is limited to the package only. This means that if we have a class with the default access modifier in a package, only those classes that are in this package can access this class. No other class outside this package can access this class. Similarly, if we have a default method or data member in a class, it would not be visible in the class of another package. Lets see an example to understand this:

Example

Variables and methods can be declared without any modifiers, as in the following examples −

String version = "1.5.1";

boolean processOrder() {
   return true;
}

Private Access Modifier – Private

The scope of private modifier is limited to the class only.

  1. Private Data members and methods are only accessible within the class
  2. Class and Interface cannot be declared as private
  3. If a class has private constructor then you cannot create the object of that class from outside of the class.

Example

The following class uses private access control −

public class Logger {
   private String format;

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

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

Here, the format variable of the Logger class is private, so there’s no way for other classes to retrieve or set its value directly.

So, to make this variable available to the outside world, we defined two public methods: getFormat(), which returns the value of format, and setFormat(String), which sets its value.

Public Access Modifier – Public

The members, methods and classes that are declared public can be accessed from anywhere. This modifier doesn’t put any restriction on the access.

Example

The following function uses public access control −

public static void main(String[] arguments) {
   // ...
}

The main() method of an application has to be public. Otherwise, it could not be called by a Java interpreter (such as java) to run the class.

Protected Access Modifier – Protected

Protected data member and method are only accessible by the classes of the same package and the subclasses present in any package. You can also say that the protected access modifier is similar to default access modifier with one exception that it has visibility in sub classes.
Classes cannot be declared protected. This access modifier is generally used in a parent child relationship.

Example

The following parent class uses protected access control, to allow its child class override openSpeaker() method −

class AudioPlayer {
   protected boolean openSpeaker(Speaker sp) {
      // implementation details
   }
}

class StreamingAudioPlayer extends AudioPlayer {
   boolean openSpeaker(Speaker sp) {
      // implementation details
   }
}

Here, if we define openSpeaker() method as private, then it would not be accessible from any other class other than AudioPlayer. If we define it as public, then it would become accessible to all the outside world. But our intention is to expose this method to its subclass only, that’s why we have used protected modifier.

Access modifiers are mainly used for encapsulation. I can help us to control what part of a program can access the members of a class. So that misuse of data can be prevented. To learn more about encapsulation