Why does the "protected" modifier in Java allow access to other classes in same package?

Uri picture Uri · May 24, 2009 · Viewed 7.8k times · Source

What is the reason that in Java, a member with a "protected" modifier can not only be accessed by the same class and by subclasses, but also by everyone in the same package?

I am wondering about language design reasons, not actual applications (e.g., testing)

Answer

Glenn picture Glenn · May 24, 2009

The modifiers are well-described at http://java.sun.com/docs/books/tutorial/java/javaOO/accesscontrol.html. From there we see this figure.

Modifier        Class     Package   Subclass  World
public          Y         Y         Y         Y
protected       Y         Y         Y         N
no modifier     Y         Y         N         N
private         Y         N         N         N

From this the reason for the design decision is obvious: it's to have a nice symmetric matrix.