Learn Java
import com.learnjava.packagesstandardlibrary.PackageAccessControl;

Access Control Across Packages in Java

@Concept

Package-private access, the default when no access modifier is written at all, means a class, field, or method is visible only to other code within the exact same package — not to subclasses in a different package, and not to any other outside code either. This makes packages a genuine access-control boundary, not just an organizational one: a class can expose a public API to the outside world while keeping its internal helper classes and methods package-private, invisible to any code living outside that specific package.

protected sits one step more permissive than package-private, additionally allowing access from subclasses even in a different package, specifically to support inheritance across package boundaries. A common design is a public class with a public API but package-private or protected internal helper methods, giving outside code only the controlled surface it's actually meant to use.

@HowItWorks
1

Package-private, the default

Visible only within the exact same package, and to nothing outside it, not even a subclass elsewhere.

2

A genuine access boundary

Packages aren't just organizational; package-private access genuinely restricts what code outside the package can reach.

3

Hiding implementation details

A package can expose a public API while keeping its internal helper classes and methods package-private.

4

protected, one step more permissive

Additionally allows access from subclasses in a different package, supporting inheritance across package boundaries.

5

A common design pattern

Public classes with a public API, paired with package-private or protected internal helpers not meant for outside use.

@Example
package com.example.util;
 
class InternalHelper {
static int helperValue() {
return 42;
}
}
 
public class PublicApi {
public static int getValue() {
return InternalHelper.helperValue();
}
 
public static void main(String[] args) {
System.out.println("Value: " + getValue());
}
}
Output
Value: 42
1InternalHelper is declared with no access modifier at all, making it package-private and invisible to any code outside com.example.util.
2PublicApi, in the same package, can freely reference InternalHelper directly, with no restriction between them.
3getValue() exposes a clean, public entry point that outside code is meant to actually use.
4The single println call shows the computed value, reached indirectly through the hidden internal helper.
@UseCases
Hiding a class's internal implementation details from code outside its own package
Exposing a clean public API while keeping helper classes and methods package-private
Allowing a subclass in a different package to access a protected method meant specifically for extension
Understanding why a package-private class can't be referenced at all from outside its package, even indirectly

Recommended resourcesAffiliate links · may earn us a commission

Recommended for learners

Structured coding coursesVideo courses that pair well with visual practice — pick one language and go deep.Browse courses Pro code editor & AI assistantWrite, debug and explain code faster while you practice the patterns here.Try it free Classic DSA interview booksThe reference books interviewers themselves learned from — great next to the visualizers.See the books

Practice what you learned

Re-implement each visualized algorithm in Java yourself — building beats watching.