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

Comparing Class-Level and Member-Level Access

Contrast the limited options for classes with the full range for members and their interactions.

Comparing Class-Level and Member-Level Access

What you'll learn: You'll understand how access control options differ dramatically between classes and their members, and why those differences matter.

The Key Difference

Java gives you limited options when controlling access to top-level classes, but provides a full range of choices for members (fields, methods, constructors). This isn't arbitrary—it reflects how Java organizes and protects code at different levels.

Class-Level Restrictions

Top-level classes can only be:

  • public — visible everywhere
  • package-private (default, no modifier) — visible only within the same package

You cannot make a top-level class private or protected. Why? Because a completely hidden class would be useless (no one could use it), and protected only makes sense in the context of inheritance and packages—which applies to members, not standalone classes.

Member-Level Flexibility

Members enjoy all four access levels:

  • private — class-only access (strongest encapsulation)
  • package-private — package-only access
  • protected — package + subclass access
  • public — unrestricted access

How They Interact

Here's the crucial point: a member cannot be more accessible than its containing class. If a class is package-private, marking a method public doesn't magically make it accessible outside the package—the class itself acts as a gatekeeper. You can't access members of a class you can't even see.

Think of it like a building: if the building (class) has restricted entry, even a "public" room (member) inside is only accessible to those who can enter the building.

Key Takeaway: Classes have only two access levels (public or package-private), while members have four; a member's effective visibility is always constrained by its class's visibility.