Course contentsShow
← Java
Lesson 704 of 2,87017. Access Control in DepthFree lesson

Package-Private (Default) Access Across Packages

Understand that package-private members are invisible outside their package, even to subclasses.

Package-Private (Default) Access Across Packages

What you'll learn: You'll understand that package-private members become completely invisible to code in other packages, even if that code is a subclass.

The Package Boundary Wall

When you declare a class member without any access modifier (making it package-private or "default" access), you're building a wall around your package. This wall is absolute: no code outside the package can see that member, regardless of any other relationship.

This is surprisingly strict. Even if you create a subclass in a different package, that subclass cannot access package-private members from its parent class. The package boundary takes precedence over the inheritance hierarchy.

Why This Matters

Think of packages as secure buildings with locked doors. Package-private members are like internal memos that never leave the building. When you move to a different package (a different building), you can't see those memos—even if you're part of the same company (inheritance hierarchy).

Here's the key principle:

  • Same package? Package-private members are visible
  • Different package? Package-private members are invisible, period
  • Different package but you're a subclass? Still invisible—the package boundary wins

This design enforces strong modularity. It ensures that the internal implementation details of a package remain truly internal, preventing external code from depending on them.

Access Summary Across Packages

When code in packageA tries to access members in packageB:

  • public members: ✓ visible
  • protected members: ✓ visible only to subclasses
  • package-private members: ✗ invisible
  • private members: ✗ invisible

Key Takeaway: Package-private members stop at the package boundary—they're invisible to all code outside their package, including subclasses in other packages.