VOOZH about

URL: https://www.geeksforgeeks.org/java/access-modifiers-java/

⇱ Access Modifiers in Java - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Access Modifiers in Java

Last Updated : 8 May, 2026

Access modifiers in Java are used to control the visibility and accessibility of classes, methods, and variables. They help enforce encapsulation by restricting access to different parts of a program. Java provides four types of access modifiers to define scope and protection levels.

  • Public modifier: It is accessible from anywhere in the program
  • Protected modifier: It is accessible within the same package and by subclasses
  • Private modifier: It is accessible only within the same class
  • Default modifier: It is accessible only within the same package

Real-World Example :In a banking app, private is used for balance (hidden), default for internal helpers, protected for methods used in subclasses, and public for actions like deposit or view details.

Types of Access Modifiers

There are 4 types of access modifiers available in Java: 

👁 Access-Modifiers-in-Java-1
Access Modifiers in Java

Private Access Modifier

The private access modifier is specified using the keyword private. The methods or data members declared as private are accessible only within the class in which they are declared.


Output
Alice

Explanation: Direct access to name is not allowed outside Person, enforcing encapsulation.

Default Access Modifier

If no access modifier is specified, the member has default (package-private) access and can only be accessed within the same package.This means only classes within the same package can access it.


Output
Tesla

Explanation: Members with default access cannot be accessed from classes in a different package.

Geeks.java: Default class within the same package

GeeksNew.java: Default class from a different package (for contrast)

Explanation: In this example, the program will show the compile-time error when we try to access a default modifier class from a different package.

Protected Access Modifier

The protected access modifier is specified using the keyword protected. The methods or data members declared as protected are accessible within the same package or subclasses in different packages.


Output
Access via subclass method: 100
0

Explanation: speed is accessible via subclass methods and other classes in the same package, but direct access from a different package (non-subclass) would fail.

Public Access Modifier

The public access modifier is specified using the keyword public. Public members are accessible from everywhere in the program. There is no restriction on the scope of public data members.


Output
15

Explanation: add() is globally accessible due to the public modifier.

Top-level classes or interfaces can not be declared as private because, private means "only visible within the enclosing class".

Comparison Table of Access Modifiers in Java

👁 same_class
Access-Modifier

When to Use Access Modifier in Real-World Projects

  • Private: The idea should be use as restrictive access as possible, so private should be used as much as possible.
  • Default (Package-Private): Often used in package-scoped utilities or helper classes.
  • Protected: Commonly used in inheritance-based designs like framework extensions.
  • Public: This is used for API endpoints, service classes, or utility methods shared across different parts of an application.
Comment
Article Tags: