blog posts

Java Access Level Controllers Training (In Plain Language)

In this tutorial, you will get acquainted with different types of Access Modifiers in Java 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 members of a class. Therefore, data misuse can be prevented.

Types of access level adjusters-Access Modifiers in Java

Access level adjusters inside the package

A package is simply a container that collects different and related types (Java classes, interfaces, counting, and annotations).

There are four keywords that 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

  1. package defaultPackage;
  2. class Logger {
  3. void message () {
  4. System.out.println (“This is a message”);
  5. }
  6. }

Here, the Logger class has a deafult access level and is available for classes that belong to the defaultPackage package. If you import the Logger class in different packages and try to introduce it, you will get an error.

2- Private access level

Only data methods and members can be declared private, while classes or interfaces cannot be declared private. However, internal classes in the nested class can be declared private.

If public receiver methods are present in the class, you can access private variables outside the class.

Example 2: Define a private access level

  1. public class Data {
  2. private String name;
  3. public String getName () {
  4. return this.name;
  5. }
  6. public void setName (String name) {
  7. this.format = name;
  8. }
  9. }
  10. public class Main {
  11. Public static void main (String [] main) {
  12. Data d = new Data ();
  13. d.setName (“Programming”);
  14. System.out.println (d.getName ());
  15. }
  16. }

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 as well as classes that are directly subclasses of your base class. Only data methods and members can be protected, while classes or interfaces cannot be protected.

Example 3: Define a protected access level

  1. // Logger.java
  2. package package1;
  3. public class Logger {
  4. protected void debug (String logLine) {
  5. System.out.println (“Debug line:” + logLine);
  6. }
  7. }
  8. // Main.java
  9. package package2;
  10. import package1.Logger;
  11. public class Main extends Logger {
  12. public static void main (String [] args) {
  13. Main logger = new Main ();
  14. // invokes debug () from Logger class
  15. logger.debug (“hello from main”);
  16. }
  17. }

Output

Debug line: hello from main

You see, Logger.java and Main.java are in different packages. The debuge () method in the Logger class is protected, and this method is 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 public access level is unlimited and can be applied to classes and interfaces with methods, data members, and variables.

Example 4: Define the public access level

  1. // Logger.java
  2. public class Logger {
  3. public int debugLevel = 1;
  4. public void debug (String logLine) {
  5. System.out.println (“Debug:” + logLine);
  6. }
  7. public void info (String logLine) {
  8. System.out.println (“Info:” + logLine);
  9. }
  10. }
  11. // LoggerImp.java
  12. public class LoggerImp {
  13. public static void main (String [] args) {
  14. Logger logger = new Logger ();
  15. logger.debug (“debug with level” + logger.debugLevel);
  16. logger.debugLevel = 5;
  17. logger.info (“info with level” + logger.debugLevel);
  18. }
  19. }

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.

C: \ Users \ Mr \ Desktop \ java-access-modifiers-public-private-protected-default_0.jpg