Java Access Modifiers

What is Access Modifier?

Access modifiers are keywords that set the accessibility (visibility) of class, interface, variable, data member, method or constructor and their setters (updating value of a variable). They are also known as visibility modifier.

You cannot set visibility modifier of getters (retrieving value of a variable) as they always take the same visibility as that of the property.

Using access modifiers forces better encapsulation to your code. Meaning, with encapsulation you can control what part of a program can access the members of a class. So that misuse of data can be prevented.

Types of Access Modifier

Access Modifiers Inside Package

A package is simply a container that groups related types (Java classes, interfaces, enumerations and annotations). Recommended reading: Java Packages

There are four access modifiers keywords in Java and they are:

ModifierDescription
PrivateDeclarations are visible within the class only
DefaultDeclarations are visible only within the package (package private)
ProtectedDeclarations are visible within the package or and all sub classes
PublicDeclarations are visible everywhere
Default Access Modifier

If the access modifier is not explicitly specified for a class, variable, method or constructor, then by default, it is assumed to be a default access modifier.

Example 1: Define Default Access Modifiers
package defaultPackage; class Logger { void message() { System.out.println(“This is a message”); } }

Here, Logger class has the default access modifier. And this logger class is visible to the classes that belong to the defaultPackage package. If you import Logger class in different package and try to instantiate it, you’ll get compilation error.

Private Access Modifier

Only the methods and the data members can be declared as private, whereas classes or interfaces cannot be declared as private. However, inner-classes in the case of a nested class can be declared private. Recommended reading: Java Nested and Inner Class

Private variables can be accessed outside the class, if public getter methods are present in the class.

Example 2: Define Private Access Modifiers
public class Data { private String name; public String getName() { return this.name; } public void setName(String name) { this.format = name; } } public class Main { Public static void main(String[] main){ Data d = new Data(); d.setName(“Cprogramcoding”); System.out.println(d.getName()); } }


Output:
Cprogramcoding

Here, name is a private variable and it is visible only inside the Data class. But, it can be accessed in another class Main by the help of public getter and setter methods.

Protected Access Modifier

Protected access modifier is accessible within same package as well as to classes that subclass your base class directly. Only the methods and the data members can be declared as protected, whereas classes or interfaces cannot be declared as protected.

Example 3: Define Protected Access Modifier
// Logger.java package package1; public class Logger { protected void debug(String logLine) { System.out.println("Debug line: "+logLine); } } // Main.java package package2; import package1.Logger; public class Main extends Logger { public static void main(String [] args){ Main logger = new Main(); // invokes debug() from Logger class logger.debug("hello from main"); } }


Output:
Debug line: hello from main

As you can see, the Logger.java and Main.java are in different package. The debug() method in Logger class in protected and this method can be accessed only inside the package1. But, however, it is accessed in the Main class as well. And this is all possible because Main class inherits the Logger class.

Public Access Modifier

Public access modifier has no any scope restriction. The public access modifier can be applied to classes and interfaces along with methods, data members and variables.

Example 4: Define Public Access Modifiers
// Logger.java public class Logger { public int debugLevel = 1; public void debug(String logLine){ System.out.println("Debug: "+logLine); } public void info(String logLine){ System.out.println("Info: "+logLine); } } // LoggerImp.java public class LoggerImp { public static void main( String[] args ) { Logger logger = new Logger(); logger.debug("debug with level " + logger.debugLevel); logger.debugLevel = 5; logger.info("info with level " + logger.debugLevel); } }


Output:
Debug: debug with level 1 Info: info with level 5

Here, in LoggerImp class, you were able to instantiate the Logger class because it’s access modifier is public. The variables and methods inside the LoggerImp class are also public. Hence, you are able to use it directly in your LoggerImp class.

All Access Modifiers summarized in one figure
Java access modifiers



Instagram