Java Access Modifiers

Master Java Access Modifiers (public, private, protected, default). Learn how to control visibility of classes, variables, and methods with real-world examples.

In Java, Access Modifiers are keywords that set the accessibility (visibility) of classes, variables, methods, and constructors.

Think of your house 🏠.

  • Public: The street outside (Everyone can see it).
  • Private: Your diary (Only you can see it).
  • Protected: Your family photos (Only family and relatives can see them).
  • Default: Your living room (Only people inside the house can see it).

Types of Access Modifiers

There are four main access modifiers in Java:

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

Modifier

Class

Package

Subclass

World

public

YesYesYesYes

protected

YesYesYesNo

default

YesYesNoNo

private

YesNoNoNo

1. Default Access Modifier

If you don't declare any modifier, it is treated as default. The default modifier is accessible only within the same package.

package com.javapedia;

class Logger {
    void message() {
        System.out.println("This is a default message");
    }
}

If you try to access Logger class from outside the com.javapedia package, you will get a compilation error.

2. Private Access Modifier

The private modifier is the most restrictive. Methods, variables, and constructors declared as private are accessible only within the declared class.

graph TD
    A[Private Member] -->|Visible| B(Inside Class)
    A -->|Hidden| C(Outside Class)
    style A fill:#f87171,stroke:#333,stroke-width:2px
    style B fill:#4ade80,stroke:#333,stroke-width:2px
    style C fill:#94a3b8,stroke:#333,stroke-width:2px

Private Access Scope

Tip 💡: Use private for sensitive data like passwords or internal logic that shouldn't be touched by others. This is the core of Encapsulation.

class Data {
    // private variable
    private String name;

    // getter method
    public String getName() {
        return this.name;
    }

    // setter method
    public void setName(String name) {
        this.name = name;
    }
}

public class Main {
    public static void main(String[] main){
        Data d = new Data();

        // access the private variable using the setter and getter
        d.setName("Java");
        System.out.println(d.getName());
    }
}

3. Protected Access Modifier

The protected modifier is accessible within the same package and subclasses (even if they are in a different package).

class Animal {
    // protected method
    protected void display() {
        System.out.println("I am an animal");
    }
}

class Dog extends Animal {
    public static void main(String[] args) {
        Dog dog = new Dog();
        // accessing protected method
        dog.display();
    }
}

4. Public Access Modifier

The public modifier is accessible from everywhere. It has the widest scope.

public class Animal {
    public int legCount;

    public void display() {
        System.out.println("I am an animal.");
        System.out.println("I have " + legCount + " legs.");
    }
}

Which access modifier makes a member visible only within its own class?


Summary

  • private: Class only. (Strictly personal)
  • default: Package only. (Neighbors)
  • protected: Package + Kids. (Family inheritance)
  • public: Everyone. (Global)

Note : Java is a statically-typed language. It means that all variables must be declared before they can be used.

Challenge

Complete this chapter to unlock the next one.

Challenge

Task:

Create a class 'Secret' with a private integer 'code' set to 1234. Create a public method 'reveal()' to print it.

Key Takeaways

  • Private: Visible ONLY inside the class.
  • Default: Visible inside the package.
  • Protected: Visible inside package + subclasses.
  • Public: Visible everywhere.

Common Pitfalls

[!WARNING] Over-sharing: Don't make everything public. Start with private and open up access only when necessary.

[!WARNING] Default vs Protected: Remember, default does NOT allow access in subclasses outside the package. protected does.

What's Next?

How do we refer to the object we are currently working inside? Learn 'this' Keyword →