Java access modifiers

Java provides many access modifiers to set the access level of classes, variables, methods and constructors. The four access levels are −
  • No keyword (no modifier specified) - visible to the package, no modifier required.
  • private- Visible only inside the class.
  • public- Public, visible to the outside world.
  • protected- Visible to the package and all subclasses.

1. Default access modifier - no keyword

Default access modifier is when no access modifier is explicitly declared for a class, field, method, etc.
A variable or method declared without any access control modifiers is available to any other class in the same package. Fields in an interface are implicit public static final, and methods in an interface default to public.

example

Variables and methods can be declared without any modifiers as shown in the following example −

class Order{
    String version = "1.0.1";

    boolean processOrder() {
       return true;
    }
}

2. Private access modifier - private

private Methods, variables and constructors declared as are only accessible within the declared class itself. The private access modifier is the most restrictive access level. Classes and interfaces cannot be declared as: private. Variables can be declared in a class
if there are public methods in the class . Using modifiers is the primary way objects encapsulate themselves and hide data from the outside world. getter private private

Example
following class uses privateaccess control −

public class Logger {
   private String format;

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

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

Here, Logger the format variable of the class is private, so other classes cannot directly retrieve or set its value.

Therefore, to make this variable available to the outside world, Logger two public methods are defined in the class: getFormat()for returning format the value, and setFormat(String)for setting its value.

 

3. Public access modifier - public

public A class, method, constructor, interface, etc. declared using can be accessed from any other class . Therefore, fields, methods, blocks declared in public classes can be accessed from any class belonging to Java.

However, if the public class you are trying to access is in a different package, you still need to import the public class. Due to class inheritance, all public methods and variables of a class are inherited by its subclasses.

example

The following methods use public access control −

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

Application main()methods must be declared as public. Otherwise the Java interpreter cannot call it to run the class.

 

4. Protected access modifier - protected

Protected variables, methods, and constructors declared in a super class can only be accessed by sub classes in other packages or by any class in the package of the protected member class.
Protected access modifiers cannot be applied to classes and interfaces. Methods, fields can be declared as protected, but methods and fields in interfaces cannot be declared as protected. Protected access gives subclasses the opportunity to use a helper method or variable while preventing unrelated classes from attempting to use it.

Example
The following parent class uses protected access control to allow its subclasses to override open Speaker() methods −

class AudioPlayer {
   protected boolean openSpeaker(Speaker sp) {
      
   }
}

class StreamingAudioPlayer {
   boolean openSpeaker(Speaker sp) {
      
   }
}
Here, if the open Speaker()method is defined as private, then it will not be AudioPlayer accessible from any other class than . If the class is defined as public, then it will be accessed by all outside world classes. But the intention here is to expose this method only to its subclasses, so only the protecte dmodifier is used.

Access Control and Inheritance

The following inheritance method rules are enforced −

  • A method declared as in a superclass publicmust also be in all subclasses public.
  • A method declared in protected a super class must also be declared in a subclass as: protected or public; cannot be declared as: private.
  • A method declared as private cannot be inherited at all, so there is no rule.