Java Lesson 26 – Access Modifiers | Dataplexa

Access Modifiers

Access modifiers in Java control who can access a class, method, or variable. They play a critical role in designing secure, maintainable, and professional Java applications.

Without proper access control, applications become difficult to manage and prone to errors. That is why access modifiers are used in almost every real-world Java project.


Real-World Understanding of Access Modifiers

Think of a company office building.

  • Some areas are open to everyone
  • Some areas are restricted to employees
  • Some rooms are accessible only to managers
  • Some information is completely private

Access modifiers work the same way in Java. They define boundaries and protect internal logic.


Types of Access Modifiers in Java

Java provides four types of access control:

  • public
  • protected
  • default (no keyword)
  • private

Each one offers a different level of visibility.


Public Access Modifier

Members declared as public are accessible from anywhere. This is the most open level of access.


public class Employee {

    public String name;

    public void showName() {
        System.out.println("Employee Name: " + name);
    }
}

Public members are commonly used for APIs and entry points.


Private Access Modifier

Members declared as private are accessible only within the same class. They are used to protect sensitive data.


class BankAccount {

    private double balance;

    public void deposit(double amount) {
        if (amount > 0) {
            balance += amount;
        }
    }

    public double getBalance() {
        return balance;
    }
}

This ensures that the balance cannot be modified directly.


Default (Package-Private) Access

When no access modifier is specified, Java uses default access. Such members are accessible only within the same package.


class Utility {

    void showMessage() {
        System.out.println("Accessible within the same package");
    }
}

Default access is useful for internal package-level logic.


Protected Access Modifier

The protected modifier allows access within the same package and also from subclasses in other packages.


class Vehicle {

    protected void start() {
        System.out.println("Vehicle is starting");
    }
}

Protected members are commonly used in inheritance-based designs.


Access Modifier Comparison

Here is a simple comparison:

  • public – accessible everywhere
  • protected – package + subclasses
  • default – package only
  • private – class only

Why Access Modifiers Matter in Real Projects

Access modifiers help:

  • Protect internal logic
  • Prevent accidental misuse
  • Improve code readability
  • Support team-based development

Frameworks like Spring and Hibernate rely heavily on correct access control.


Best Practices

  • Use private for variables by default
  • Expose behavior using public methods
  • Limit visibility as much as possible
  • Design APIs intentionally

Key Takeaways

  • Access modifiers control visibility in Java
  • They improve security and maintainability
  • They are essential for clean object-oriented design

In the next lesson, we will explore the static keyword and understand how it changes class behavior.