Access Modifiers

Why can't you touch private data directly? Master Access Modifiers to build safe and predictable software systems.

April 16, 20264 min read3 / 14

In our post on Encapsulation, we learned about "capsules." Now, let's look at the specific tools we use to build those capsule walls. These tools are called Access Modifiers.

The Essentials

The "Gatekeeper's Guide" to this post:

  1. Information Hiding: The goal is to hide "How" things work and only show "What" can be done.
  2. Predictability: When data is private, you know exactly which methods can change it.
  3. The Four Levels: Java provides Private, Default, Protected, and Public levels of access.
  4. Member vs Class: Modifiers can be applied to both data (attributes) and actions (methods).

Why Hiding Matters: The Heart Analogy

Think about your own body. You have a heart, lungs, and a brain.

  1. They are Private: No one can reach in and touch your heart directly. If they could, it would be extremely dangerous and unpredictable.
  2. They have a Public Interface: You can check your heart rate by feeling your pulse. The "Pulse" is the public gateway to the private data of your heart.

Software works the same way. If you let any piece of code touch a Student's psp directly, you can never be sure if the score is valid. If you make it private and provide an updateScore() method, you are in control.

The Java Hierarchy: Security Clearances

In Java, we have four distinct levels. Imagine a corporate office:

  1. Private (Your Desk): Only you can see what's in your drawer. (Same Class)
  2. Default (The Department): Everyone in your department can see the shared whiteboard. (Same Package)
  3. Protected (The Family): Your children can see your family photos even if they live in a different house. (Subclasses)
  4. Public (The Lobby): Anyone walking in off the street can see the company logo. (Everywhere)

Access Modifiers in Action: The Gatekeeper Problem

Public (The Lobby)

Data is public. Anyone can see it and anyone can change it. There is zero privacy and zero control.

class Locker {
  public secret: string;
}

// Anyone can open it

Like a company logo in the lobby. It's visible to everyone, including strangers off the street.

Private (Your Desk)

Data is private. Only the owner can see what's inside. Others must ask for permission via public methods.

class Locker {
  private secret: string;
  public getSecret() { .. }
}

Like a locked drawer in your desk. You have the only key. This is the foundation of secure LLD.

Code Implementation: Setting the Clearances

Here is how you apply different levels of access to your class members:

class Employee { // Private: Only this class can see it private salary: number; // Public: Everyone can see it public name: string; // Protected: Only this class and its children can see it protected performanceRating: number; constructor(name: string, salary: number) { this.name = name; this.salary = salary; } public getSalaryInfo() { return `Name: ${this.name}`; // Cannot return private salary directly! } }

By choosing the right level of access, you ensure that your code is not only secure but also easy for your teammates to use without breaking things.

Now that we know how to protect our data, let's learn about Constructors: the ritual that sets everything up.

Practice what you just read.

Protecting the TV: Access ModifiersInheritance: Point Dimensions
2 exercises