March 28, 2026By Srushti Kulkarni

Java Access Modifiers Explained


Java is an object-oriented programming language that emphasizes data security and controlled access. One of the key concepts that helps achieve this is Access Modifiers. They define the visibility or accessibility of classes, methods, variables, and constructors in a Java program.


In this blog, we will explore Java Access Modifiers in a simple and practical way, along with diagrams to help you understand clearly.


What are Access Modifiers?

Access Modifiers in Java are keywords used to control the visibility of classes and their members. They determine who can access what in your code.

Java provides four types of access modifiers:

  1. Default (No Modifier)
  2. Private
  3. Protected
  4. Public


🔹 Why are Access Modifiers Important?

Access modifiers help in:

  • Data hiding (Encapsulation)
  • Security of sensitive data
  • Controlling unwanted access
  • Improving code maintainability


🔹 Types of Access Modifiers

Let’s understand each modifier in detail.


1️⃣ Default Access Modifier

When no access modifier is specified, it is considered default.

✅ Accessibility:

  • Accessible only within the same package
  • Not accessible outside the package

📌 Example:

class Demo {

    int x = 10;  // default access

}


📊 Diagram:

Same Package      →  ✅ Accessible

Different Package →  ❌ Not Accessible


2️⃣ Private Access Modifier

The private modifier is the most restrictive.

✅ Accessibility:

  • Accessible only within the same class
  • Not accessible outside the class

📌 Example:

class Demo {

    private int x = 10;


    void show() {

        System.out.println(x); // accessible here

    }

}


📊 Diagram:

Same Class        →  ✅ Accessible

Same Package      →  ❌ Not Accessible

Different Package →  ❌ Not Accessible


💡 Use Case:

Used for data hiding in encapsulation.


3️⃣ Protected Access Modifier

The protected modifier is slightly more open than the default.

✅ Accessibility:

  • Accessible within the same package
  • Accessible in subclasses (even in different packages)

📌 Example:

class Parent {

    protected int x = 10;

}


class Child extends Parent {

    void show() {

        System.out.println(x); // accessible

    }

}


📊 Diagram:

Same Class             →  ✅ Accessible

Same Package           →  ✅ Accessible

Different Package      →  ❌ Not Accessible

Subclass (Other Pkg)   →  ✅ Accessible


4️⃣ Public Access Modifier

The public modifier is the least restrictive.

✅ Accessibility:

  • Accessible from anywhere


📌 Example:

public class Demo {

    public int x = 10;

}


📊 Diagram:

Same Class        →  ✅ Accessible

Same Package      →  ✅ Accessible

Different Package →  ✅ Accessible

Anywhere          →  ✅ Accessible

🔹 Summary Table


🔹 Real-Life Analogy

Think of access modifiers like privacy settings in social media:

  • Private → Only you can see
  • Default → Only friends (same package)
  • Protected → Friends + close relatives (subclasses)
  • Public → Everyone


🔹 Important Notes

  • You cannot declare a class as private or protected (only nested classes can).
  • Top-level classes can be:
  • Public
  • Default
  • Variables, methods, and constructors can use all modifiers.


🔹 Practical Example

Let’s see a simple program combining all modifiers:

class Example {


    private int a = 1;

    int b = 2; // default

    protected int c = 3;

    public int d = 4;


    void display() {

        System.out.println(a + " " + b + " " + c + " " + d);

    }

}


🔹 When to Use Which?

Use private → Never access data directly

default use → Within the same package

protected → When inheritance comes into the picture

Use public → When data has to be accessible everywhere


🔹 Best Practices

✔ Use getters/setters for proper data encapsulation

✔ Avoid using public unnecessarily

✔ Use protected only if inheritance is necessary

✔ Follow proper encapsulation principles

🔹 Conclusion

Why Use Java Access Modifiers? There are plenty of reasons to use access modifiers while writing secure, maintainable, and well-structured code. This will assist in implementing encapsulation and limiting accessibility of sensitive data.

Knowing the context in which to apply each of these modifiers is one of the essential skills for any Java developer. When you get this concept, your code will be more professional and extensive.


Frequently Asked Questions (FAQs):


1. Java Access Modifiers Explanation

Java Access Modifiers are the keywords used to define the visibility or accessibility of classes, methods, and variables. They assist in managing access or modification of data by various segments of a program.


2. Access Modifiers in Java /* Access modifiers are classified into four types:*/

Java access modifiers have four types:

public – accessible from anywhere

protected – available to the same package and subclasses

default (no modifier) – visible within the same package only

private – available exclusively within the same class


3. Explain the difference between public and private access modifiers.

The least restrictive of the three, and also allows access from any class in any package, is the public modifier. On the other hand, a private modifier allows access only from within the class in which it is declared, making it the most restrictive.


4. What is a protected access modifier in Java, and when should you use it?

Protected Modifier: When do you want to allow access outside the same package and provide access to subclasses of other packages? It is most commonly found in inheritance, where it helps us to share data or methods with child classes.


5. If you have seen the keywords public, private, or any other access modifiers in Java, but do not understand why we would even need them?

Introduction to Access Modifiers: Why are access modifiers important? This enables the filtering of which component has access to which other components in order to keep a clean architecture.


Related Links:

Advantages and Disadvantages of AI

Top 50 AI Tools Lists

AI Engineer Roadmap


Do visit our channel to know more: SevenMentor

Author:-

Srushti Kulkarni

Srushti Kulkarni

Expert trainer and consultant at SevenMentor with years of industry experience. Passionate about sharing knowledge and empowering the next generation of tech leaders.

#Technology#Education#Career Guidance