Java - Access Modifiers
Java - Access Modifiers
Java access modifiers are used to specify the scope of the variables, data members, methods,
classes, or constructors. These help to restrict and secure the access (or, level of access) of the
data.
There are four different types of access modifiers in Java, we have listed them as follows:
A variable or method declared without any access control modifier is available to any other class in
the same package. The fields in an interface are implicitly public static final and the methods in an
interface are by default public.
boolean processOrder() {
return true;
}
Private access modifier is the most restrictive access level. Class and interfaces cannot be private.
Variables that are declared private can be accessed outside the class, if public getter methods are
present in the class.
Using the private modifier is the main way that an object encapsulates itself and hides data from
the outside world.
Example 1
The following class uses private access control −
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.
Example 2
In this example, the data members and class methods of the Logger class are private. We are
trying to access those class methods in another class Main.
Open Compiler
class Logger {
private String format;
private String getFormat() {
return this.format;
}
}
}
Output
Learn Java in-depth with real-world projects through our Java certification course. Enroll and
become a certified expert to boost your career.
The protected access modifier cannot be applied to class and interfaces. Methods, fields can be
declared protected, however methods and fields in a interface cannot be declared protected.
Protected access gives the subclass a chance to use the helper method or variable, while
preventing a nonrelated class from trying to use it.
Example 1
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
}
}
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.
Example 1
This example demonstrates the use of protected access modifier.
Open Compiler
// Class One
class One {
protected void printOne() {
System.out.println("printOne method of One class.");
}
}
Output
However, if the public class we are trying to access is in a different package, then the public class
still needs to be imported. Because of class inheritance, all public methods and variables of a class
are inherited by its subclasses.
Syntax
The following function uses public access control −
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.
Open Compiler
// Class One
class One {
public void printOne() {
System.out.println("printOne method of One class.");
}
}
Output
The following table shows the summary of the accessibility in the same/different classes (or,
packages) based on the access modifiers.
public Puppy() {
}
Output