blog posts

Access Level

What Is Java Access Level Controllers — Understanding Java’s Access Modifiers

In this tutorial, you will learn about the different types of Java access Modifiers and how they work in various 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:

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 defaults to 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 default access level and is available to classes in the defaultPackage package. You will get an error if you import the Logger class in different packages and try to use it.

2- Private access level

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

You can access private variables outside the class if the class has public receiver methods.

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 and classes that are direct subclasses of your base class. Data members and methods can be protected, whereas classes or interfaces cannot.

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

Logger.java and Main.java are in different packages. The debug() method in the Logger class is protected and accessible only within the package. However, it is also available in the Main class because Main inherits from Logger.

4- Public access level

The unlimited public access level applies to classes and interfaces, including 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 a Logger instance because Logger is public. Variables and methods within the LoggerImp class are also public. Hence, you can use it directly in the LoggerImp class.

Here, in the LoggerImp class, you were able to create a Logger instance because Logger 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, base subclass, 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: default access is more restrictive than protected access, because protected members can be accessed within subclasses.

.

FAQ

What are access modifiers (access level controllers) in Java?

They are keywords that define who can access a class, method, or variable — controlling visibility across the application.

Which access levels exist in Java?

Java supports four access levels: public, protected, package-private (default, no modifier), and private.

When should I choose each access level?

Use private to hide implementation details within a class; use default (package-private) to restrict access to the same package; use protected when subclasses should access members; and use public for API-level methods or classes accessible from anywhere.