Free tutorials & notes in Hindi & English · Clean code examples 🧰 Free PDF & Image Tools →
📘 Lesson  ·  Lesson 33

Access Modifiers in Java

In this lesson: Java access modifiers explained with a visibility table — public, private, protected, default — and examples. In Hindi & English.

What are access modifiers

Access modifiers control who can use a class, field or method. They let you hide internal details and expose only what should be public. Java has four levels, from fully open (public) to fully closed (private). They are the foundation of encapsulation.

💡 public = everywhere, private = same class only, protected = package + subclasses, default = same package.

The four modifiers

  • public — accessible from anywhere.
  • private — accessible only within the same class.
  • protected — accessible in the same package and by subclasses.
  • default (no keyword) — accessible only within the same package.

You choose the most restrictive level that still lets your code work — this keeps data safe.

private and encapsulation

class Account {
    private double balance = 0;   // hidden data
}
// Account a = new Account();
// a.balance = -500;   // ERROR: balance is private

Making balance private stops outside code from changing it directly — for example setting a negative balance. This is encapsulation: the field is hidden and protected.

Getters and setters

class Account {
    private double balance = 0;

    public double getBalance() {      // getter
        return balance;
    }
    public void deposit(double amt) { // controlled setter
        if (amt > 0) balance += amt;  // validation!
    }
}

Public methods give controlled access to a private field. The deposit method validates the amount before changing balance — something you cannot enforce with a public field.

protected and default

  • protected — used when subclasses (even in other packages) need access, but the public should not.
  • default — written by using no modifier; the member is visible only inside its own package.

These middle levels are less common than public and private, but useful for package and inheritance design.

Quick comparison

ModifierSame classSame packageSubclassEverywhere
publicYesYesYesYes
protectedYesYesYesNo
defaultYesYesNoNo
privateYesNoNoNo

Common mistakes

  • Making all fields public. Keep fields private and expose them via methods.
  • Confusing default with public. No modifier means package-private, not public.
  • Overusing protected. Use it only when subclasses genuinely need access.
  • Skipping validation in setters. The point of a setter is to check values before setting.

Summary

  • Access modifiers control who can use a class member.
  • public is open everywhere; private is same-class only.
  • protected adds subclasses; default is package-only.
  • Keep fields private and expose them through getters/setters — encapsulation.
  • Setters can validate values before changing private data.

Next, revise data hiding with classes and objects, or learn the static keyword.

Frequently Asked Questions

What are access modifiers in Java?
Access modifiers control who can see and use a class, field or method. Java has four: public (everywhere), private (same class only), protected (same package plus subclasses), and default (same package). They protect your code.
What is the difference between public and private?
public means the member can be accessed from anywhere. private means only the same class can access it. Fields are usually private, and public methods control access to them.
What is encapsulation?
Encapsulation means keeping fields private and exposing them only through public methods (getters and setters). This hides internal data and lets you validate changes, which is a core benefit of access modifiers.
What is the default access modifier?
If you write no modifier, the member has default (package-private) access — visible only to classes in the same package. It is more restrictive than protected and public.
What is a getter and setter?
A getter is a public method that returns a private field's value (like getName()), and a setter is a public method that changes it (like setName()). They give controlled access to private data.
← Back to Java Tutorial
🔗

Share this topic with a friend

यह topic किसी दोस्त को भेजें

Found it useful? Send it to a classmate learning the same thing.

अच्छा लगा? जो दोस्त यही सीख रहा है, उसे भेज दीजिए।

💻 Live Code Editor

This page's programs are ready here — run them, edit them, and learn. No installation needed.
Powered by OneCompiler. The code loads into the editor automatically — press Run to see the output. If the editor does not open, open it in a new tab.
Next topicWhy No Multiple Inheritance Your progress: 33 / 61 (54%) · Lesson 34 / 61 Continue →

How was this lesson?

Your feedback helps us make the content better.

Write a review or suggestion (optional)

After approval, your name and city may appear publicly with your review. Please do not write your email or phone number.

▶ CodingEasily on YouTube

Learn with video lessons in easy Hindi

Featured video: RDBMS Part 3 – Class XI Commerce IT

▶ Subscribe – it’s free