Java Access Modifiers


Java offers numerous access modifiers to set admission levels for groups, variables, techniques, and constructors. The four contact levels are −

  • Noticeable to the wrap-up, the default
  • Observable to the course group only (private)
  • Perceptible to the globe (public)
  • Evident to the pack up and all subclasses

Default Access Modifier - No Keyword

Default way in modifier signifies we do not clearly announce an access modifier for a class, arena, method, etc.

A changeable or technique stated with no contact control modifier is obtainable to any other group in the identical package. The fields in a line are absolutely public static last and the ways in a line are by default public.


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

Methods, variables, and constructors that are announced private can only contact within the asserted class itself.

The private entrance modifier is the mainly restrictive access stage. Class and lines cannot be personal.

Variables that are asserted private can be accessed exterior the class if open getter methods are here in the class.

Utilizing the private modifier is the core way that an object sum ups itself and hides data from the exterior world.


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 changeable of the Logger group is personal, so there is no method for other classes to recover or set its worth directly.

So, to create this changeable accessible to the exterior world, we named two civic methods: getFormat(), which revisits the value of format, and setFormat(String), which sets its worth.

Public Access Modifier - Public

A class, process, constructor, line, etc. announced public can be accessed from any other group. Consequently, fields, methods, hunks announced inside a public class can be an admission from any class fitting into the Java Universe.

However, if the public group we are annoying to admission is in a diverse wrap-up, then the public set still wants to be introduced. Because of class legacy, all public techniques and changeable of a class are be left by its subclasses.


The following function uses public access control −

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

Protected Access Modifier - Protected

Variables, ways, and constructors, which are declared protected in a superclass, can be accessed only by the subclasses in other enclose or any class in the package of the defended members' class.

The protected access modifier never applied to class and lines. Methods, fields can be stated protected, however processes and fields in a line cannot be declared protected.

Therefore, the protected way gives the subclasses a possibility to use the helper method. Moreover, the variable, while stopping a nonrelated class from attempting to use it.


The following parent class uses protected access control, to allow its child class to 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 describe openSpeaker() technique as private, then it would not be easy to get to from any other group other than AudioPlayer. If we describe it as public, then it would turn out to be nearby to entire the outside world. However, our meaning is to depict this technique to its subclass only, that’s why we have utilized a protected modifier.

Access Control and Inheritance

The subsequent rules for succeeded methods are put into effect:

  • Methods stated public in a superclass also has to be public in entire subclasses.
  • Methods announced defended in a superclass must be either protected or public in subclasses; they never are private.
  • Methods announced private are not accede to at entire, so there is no decree for them.

Here at Intellinuts, we have created a complete Java tutorial for Beginners to get started in Java.