Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does Java have a "private protected" access modifier?

People also ask

What is private protected access modifier in Java?

The private modifier specifies that the member can only be accessed in its own class. The protected modifier specifies that the member can only be accessed within its own package (as with package-private) and, in addition, by a subclass of its class in another package.

Does Java have protected access?

The protected access modifier is accessible within package and outside the package but through inheritance only. The protected access modifier can be applied on the data member, method and constructor. It can't be applied on the class. It provides more accessibility than the default modifer.

Is protected a modifier in Java?

protected: The protected access modifier is specified using the keyword protected. The methods or data members declared as protected are accessible within the same package or subclasses in different packages.

Why protected modifier is not allowed in Java?

what is the reason? You cannot use the protected access modifier for top level classes. The protected modifier means that the member can only be accessed within its own package or by a subclass of its class in another package. At top level you can only use the public modifier or no modifier (package-private).


Removal of the access modifier

Java did originally have the private protected modifier, but it was removed in JDK 1.0.2 (the first stable version, the Java 1.0 we know today). A few tutorials regarding JDK 1.0.2 (here and here) say the following:

Note: The 1.0 release of the Java language supported five access levels: the four listed above plus private protected. The private protected access level is not supported in versions of Java higher than 1.0; you should no longer be using it in your Java programs.

Another answer on SoftwareEngineering.SE states:

Java originally had such a modifier. It was written private protected but removed in Java 1.0.

Now take a look at the Java Version History:

JDK 1.0

The first version was released on January 23, 1996 and called Oak. The first stable version, JDK 1.0.2, is called Java 1.

From this, we can conclude the tutorials regarding version 1.0.2 refer to the very first version, JDK 1.0, where the language was called Oak, but the one from SoftwareEngineering.SE refers to the first stable version, JDK 1.0.2 called Java 1.0, where it was removed.

Now if you try to search for it in the Java 1.0 documentation, you won't find it, because as mentioned earlier, it was removed in JDK 1.0.2, otherwise known as Java 1.0. This is proven again when you look at the "Last Modified" times for the link you posted. The link you posted was last modified in February of 1996. Java 1.0/JDK 1.0.2, when private protected was removed, was released after February of 1996, and according to the specification, August of 1996.

Reason for removal

Some sources also explain the reason for private protected, such as this one. To quote:

What was private protected?

Early on, the Java language allowed for certain combinations of modifiers, one of which was private protected. The meaning of private protected was to limit visibility strictly to subclasses (and remove package access). This was later deemed somewhat inconsistent and overly complex and is no longer supported.[5]

[5] The meaning of the protected modifier changed in the Beta2 release of Java, and the private protected combination appeared at the same time. They patched some potential security holes, but confused many people.

And the SoftwareEngineering.SE also supports this, by saying that it wasn't worth the inconsistencies and extra complexity, so it was removed early on.

Interpretation

My interpretation of all this is that maybe, back in the Oak days, both were allowed to coexist (hence the combination). Since protected's meaning had changed1, there may have been a need for allowing private and protected at the same time. The introduction became too complex and wasn't worth it, and was thus dropped in the end. By the time Java 1.0/JDK 1.0.2 rolled around, it had been dropped and thus cannot be found in the documentation.


1In the Oak Language Specification, Section 4.10, Access to Variables and Methods, it is noted that the default modifier was protected:

By default all variables and methods in a class are protected.

This is quite different from what we have today, the default package access. This may have paved the way for the need of private protected, because private was too restrictive and protected was too lenient.


There are confusing/unclear stories:

One, from the Princeton source you put, and also from MIT archives, states that:

Note: The 1.0 release of the Java language supported five access levels: the four listed above plus private protected. The private protected access level is not supported in versions of Java higher than 1.0; you should no longer be using it in your Java programs.

But this feature is not specified on any official documentation for Java 1.0 here or here.

My guess is that this feature didn't make it to the official 1.0 version, since the official language specification is from August 1996 and Princeton source was last modified on February 1996.

PS: shame on Oracle for removing the archives for older versions.


As the link you provided in your question suggests private protected was used on an element/member of a class, when you want your subclass to be able access the element but keep it hidden from other classes in its package.

Java if compared to C++ has an extra concept of encapsulating elements - and that is a Package. One should also understand what is accessible within or outside a package in Java when it comes to these access-specifiers like private, public & protected.

Please note that I have explained why it was used. Not in current version of course