Java Access Modifiers

Access modifiers decide who can see and use a class, a variable, or a method. They protect your data and keep your program tidy. Java offers four levels: private, default, protected, and public.

An Office Building Example

Picture an office building. Your desk drawer opens only for you. Your department floor opens for your team. The company guest lounge welcomes anyone who enters the building.

  • private works like the desk drawer
  • default works like the department floor
  • protected works like the department floor plus related branch offices
  • public works like the lobby

The Four Levels

private

Only code inside the same class can use a private member. Use private for data that outsiders must not touch.

default (no keyword)

Skip the keyword and Java applies default access. Code in the same package can use the member.

protected

Code in the same package can use a protected member. Child classes in other packages can use it as well.

public

Every class in every package can use a public member.

Access Table

ModifierSame ClassSame PackageChild in Other PackageEverywhere
privateYesNoNoNo
defaultYesYesNoNo
protectedYesYesYesNo
publicYesYesYesYes

Diagram: Circles of Access

+--------------------------------------------------+
| public: every class                              |
|   +------------------------------------------+   |
|   | protected: package + child classes       |   |
|   |   +----------------------------------+   |   |
|   |   | default: same package            |   |   |
|   |   |   +--------------------------+   |   |   |
|   |   |   | private: same class      |   |   |   |
|   |   |   +--------------------------+   |   |   |
|   |   +----------------------------------+   |   |
|   +------------------------------------------+   |
+--------------------------------------------------+

Code Example

package bank;

public class Account {
    private double balance = 500;      // hidden
    int branchCode = 12;               // package only
    protected String type = "Savings"; // package + children
    public String owner = "Meera";     // everyone

    public double getBalance() {
        return balance;
    }
}

Code in another package reads owner and calls getBalance. That code cannot read balance directly. The class keeps the money safe and offers a public method for safe reading.

Access Rules for Classes

A top-level class uses only public or default access. A public class must live in a file with the same name. Inner classes can use all four levels.

Best Practice

Start with the strictest level. Mark variables private and open only what other classes truly need. Small public surfaces make code easier to change later. This habit leads straight into the idea of encapsulation.

Common Mistakes

  • Making every variable public for convenience
  • Expecting a private method to work in a child class
  • Forgetting that default access ends at the package border
  • Putting two public classes in one file

Real Example: A Student Record

A school program stores each student's marks. The marks stay private so no outside code can raise a grade by mistake. A public method sets the marks and checks that the number is valid.

public class Student {
    private int marks;

    public void setMarks(int value) {
        if (value >= 0 && value <= 100) {
            marks = value;
        }
    }
    public int getMarks() {
        return marks;
    }
}

A call such as student.setMarks(150) changes nothing. The class guards its own data. This pattern protects every bank, school, and shop application.

Quick Summary

  • private limits access to the class
  • default limits access to the package
  • protected adds child classes
  • public opens access to all