S07L22 – Access modifiers in Java – Getting started

Understanding Access Modifiers in Java

Table of Contents

  • Introduction
  • What Are Access Modifiers in Java?
  • Types of Access Modifiers
  • Access Modifier Usage in Different Scenarios
  • Example Code for Access Modifiers
  • Conclusion

1. Introduction

In Java, access modifiers control the visibility and accessibility of classes, methods, variables, and constructors.
They define the scope of access that other classes have to a particular resource. Understanding access modifiers is essential
in building secure, maintainable, and well-structured code. This article will explain the different types of access modifiers in Java,
how they work, and when to use them.

2. What Are Access Modifiers in Java?

Access modifiers, sometimes referred to as access specifiers or scope specifiers, dictate where a particular class, method,
or variable can be accessed from within a Java application. They help in controlling the exposure of class members (variables and methods),
ensuring that only certain parts of your code can interact with sensitive data or specific logic.

The four main access modifiers in Java are:

  • private
  • default (or no modifier)
  • protected
  • public

3. Types of Access Modifiers

The following table provides an overview of the four types of access modifiers and their scope:

Modifier Description
private Element accessible only within the class.
default / no modifier No modifier is treated as the default. The element is accessible only within the package.
protected Accessible within the package and outside the package through inheritance only.
public The public modifier is accessible everywhere, offering the widest scope of all modifiers.

Here is a visual representation of access modifiers and their scope:

4. Access Modifier Usage in Different Scenarios

To further clarify the differences in access levels for each modifier, the following table provides a more detailed comparison:

default private protected public
Same Class Yes Yes Yes Yes
Same Package Subclass Yes No Yes Yes
Same Package Non-Subclass Yes No Yes Yes
Different Package Subclass No No Yes Yes
Different Package Non-Subclass No No No Yes

5. Example Code for Access Modifiers

Here is a simple example demonstrating the use of access modifiers in a Java class:

Explanation:

  • privateVar: This variable is accessible only within the Example class. It cannot be accessed outside this class.
  • defaultVar: Since no modifier is provided, this variable has package-level access.
  • protectedVar: This variable is accessible within the same package or via inheritance in a subclass located in another package.
  • publicVar: This variable is accessible from any other class.

6. Conclusion

Understanding access modifiers in Java is crucial for writing secure and well-structured applications. The correct use of access modifiers helps
in controlling the visibility of class members and restricts unnecessary access.

In this article, we discussed the four main access modifiers:private, default, protected, and public.

Each has a specific role and scope and using them appropriately ensures encapsulation and proper data protection.

For more complex projects, combining access modifiers with other design patterns helps in building scalable and secure Java applications.