Access Control Across Packages in Java
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.
Package-private, the default
Visible only within the exact same package, and to nothing outside it, not even a subclass elsewhere.
A genuine access boundary
Packages aren't just organizational; package-private access genuinely restricts what code outside the package can reach.
Hiding implementation details
A package can expose a public API while keeping its internal helper classes and methods package-private.
protected, one step more permissive
Additionally allows access from subclasses in a different package, supporting inheritance across package boundaries.
A common design pattern
Public classes with a public API, paired with package-private or protected internal helpers not meant for outside use.
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()); }}Recommended resourcesAffiliate links · may earn us a commission
Recommended for learners
Practice what you learned
Re-implement each visualized algorithm in Java yourself — building beats watching.