Java Access Level Controllers Training
In this tutorial, you will learn about the different types of Java access Modifiers and how they work in different scenarios.
What is an Access Modifier?
Access level controllers are keywords that determine the accessibility (access rate) of a class, interface, variable, data member, method, or constructor. They are also known as visibility modifiers.
You can encapsulate the code using access level adjusters. This means specifying what part of a program can access class members. Therefore, data misuse can be prevented.
Types of access level adjusters- Access Modifiers in Java
Access level adjusters inside the package
A package container collects different and related types (Java classes, interfaces, counting, and annotations).
Four keywords regulate access levels in Java:
All access level regulators are described in detail below.
1. Default access level
If the access level regulator is not explicitly specified for a class, variable, method, or constructor, the access level is assumed to be the default.
Example 1: Define the default access level
- package defaultPackage;
- class Logger {
- void message () {
- System.out.println (“This is a message”);
- }
- }
Here, the Logger class has a deafult access level and is available for classes that belong to the defaultPackage package. You will get an error if you import the Logger class in different packages and try to introduce it.
2- Private access level
Only data methods and members can be declared private, while classes or interfaces cannot. However, internal classes in the nested class can be declared private.
You can access private variables outside the class if public receiver methods exist.
Example 2: Define a private access level
- 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 (“Programming”);
- System.out.println (d.getName ());
- }
- }
Output
Programiz
Here, name is a private variable and is only accessible within the Data class. However, with the help of public receiver methods, it can be accessed in the Main class.
3- Access level protected
The protected access level provides access to the same package and classes that are direct subclasses of your base class. Data methods and members can be protected, while classes or interfaces cannot.
Example 3: Define a protected access level
- // 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
Logger.java and Main.java are in different packages. The debug () method in the Logger class is protected and only accessible inside the package. However, it is also available in the Main class because the Main class inherits from the Logger class.
4- Public access level
The unlimited public access level can be applied to classes and interfaces with methods, data members, and variables.
Example 4: Define the public access level
- // 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 the LoggerImp class, you were able to create the Logger class because it is public. Variables and methods within the LoggerImp class are also public. Hence, you can use it directly in the LoggerImp class.
The TestE
class, basa subclassTestB
, allows access to the protected members TestB
through inheritance. However, invoking a protected superclass method directly will result in a compile-time error.
Java access modifiers are straightforward once you grasp the basics. It’s important not to mix up default and protected access levels. A helpful tip to remember: default access is more restrictive than protected, as protected members can be accessed within subclasses.
.