Yandex

Java Advanced ConceptsJava Advanced Concepts3

Java ReferenceJava Reference1

Java Access Modifiers
Explanation and Examples



In Java, access modifiers are keywords that set the visibility or accessibility of classes, methods, constructors, and variables. They help define who can see or use a particular piece of code. Think of them like the security levels of a building—some areas are open to all, while others are restricted.

Access Modifiers promote encapsulation. By carefully selecting access levels, you keep your code modular, maintainable, and secure from unintended interference.

The Four Types of Access Modifiers in Java

1. public – Open to All

When a class or method is marked public, it’s like declaring it as “open for business.” Any class from any package can access it.

public class MyClass {
    public void greet() {
        System.out.println("Hello from a public method!");
    }
}
public class Main {
    public static void main(String[] args) {
        MyClass obj = new MyClass();
        obj.greet();
    }
}
Hello from a public method!

2. private – Hidden From the World

Private members are strictly confined to the class they belong to. No external class can access them—not even a subclass.

public class SecretVault {
    private String secretCode = "1234";

    private void whisper() {
        System.out.println("Accessing private secrets...");
    }

    public void reveal() {
        whisper();
        System.out.println("Code is: " + secretCode);
    }
}
public class AccessAttempt {
    public static void main(String[] args) {
        SecretVault vault = new SecretVault();
        vault.reveal();
        // vault.secretCode;     // Error
        // vault.whisper();      // Error
    }
}
Accessing private secrets...
Code is: 1234

Trying to access secretCode or whisper() directly will throw a compile-time error. They’re truly off-limits.

3. protected – For Family and Friends

Protected members can be accessed from:

// In package animals
package animals;

public class Animal {
    protected void makeSound() {
        System.out.println("Animal makes a sound");
    }
}
// In package zoo
package zoo;
import animals.Animal;

public class Dog extends Animal {
    public void bark() {
        makeSound(); // Allowed due to protected
        System.out.println("Dog barks!");
    }
}
public class Main {
    public static void main(String[] args) {
        Dog dog = new Dog();
        dog.bark();
    }
}
Animal makes a sound
Dog barks!

Here, the subclass Dog accesses makeSound() from a different package because it's protected.

4. default (Package-Private) – Locally Visible

When no modifier is specified, Java treats it as default access. Such members are only visible within the same package.

// In file PackageClass.java
package mypackage;

class PackageClass {
    void display() {
        System.out.println("Default access: only in mypackage");
    }
}
// In another file in same package
package mypackage;

public class Demo {
    public static void main(String[] args) {
        PackageClass pc = new PackageClass();
        pc.display(); // Works fine
    }
}
Default access: only in mypackage

If another class in a different package tries to use PackageClass or call display(), it will result in an error.

Access Modifier Summary Table

Modifier Same Class Same Package Subclass (Diff Package) Other Packages
public Yes Yes Yes Yes
protected Yes Yes Yes No
default Yes Yes No No
private Yes No No No

When to Use What?

Here’s a quick decision guide:

QUIZ

Question 1:Which access modifier allows a class member to be accessed from any other class in any package?

Question 2:A private method can be accessed by a subclass in a different package.

Question 3:Which of the following statements about protected access are correct?

Question 4:What is the accessibility of a class member that has no access modifier defined?

Question 5:Using public access modifier helps in hiding implementation details from other classes.

Question 6:Which of the following members can be accessed from outside the package without inheritance?



Welcome to ProgramGuru

Sign up to start your journey with us

Support ProgramGuru.org

You can support this website with a contribution of your choice.

When making a contribution, mention your name, and programguru.org in the message. Your name shall be displayed in the sponsors list.

PayPal

UPI

PhonePe QR

MALLIKARJUNA M