162

I have seen some references refer to a access modifier in Java called private protected (both words together):

private protected someMethod() {

}

One of the pages I found referring to this is here. My school lesson also referred to this access modifier (and said it exists). Using it, however, results in an error in the Java language.

I tried with both variables and methods and I'm pretty sure it doesn't exist, but I want an explanation of what happened. Was it considered, then rejected? Or did it get removed in a newer version of Java?

Edit: I am not looking for info about the protected keyword.

  • 61
    The page you found sets a "Last-Modified" HTTP header of: Mon, 26 Feb 1996 18:14:04 GMT ! – G. Sylvie Davies Jan 02 '17 at 18:28
  • @G.SylvieDavies I didn't notice that ! :D but I thought it seemed weird that I never saw it in modern code –  Jan 02 '17 at 18:29
  • 1
    Possible duplicate of [Why is Java's "protected" less protected than default?](http://stackoverflow.com/questions/15626321/why-is-javas-protected-less-protected-than-default) – Joe Jan 03 '17 at 08:44
  • 6
    @Joe I'm all for closing questions as dupes when possible, but I don't see *anything* about a combined `private protected` modifier in there. – jpmc26 Jan 03 '17 at 11:34
  • 3
    @jpmc26 See "In Java 1.0 there was an additional access modifier, private protected." However, the answer here is a much better summary of the history. – Joe Jan 03 '17 at 11:38
  • 2
    @Joe There's indeed a reference to `private protected` in that answer, but it doesn't explain why or what happened to it, which what this question is about. – m0skit0 Jan 03 '17 at 18:15
  • 3
    Does anyone else find it scary that the OP was learning this in school.... over 20 years after it was removed from the Docs? Interesting history lesson, but still a bit scary that people are learning something that was removed before Java 1 was named... – XaolingBao Jan 04 '17 at 13:27
  • I am a bit confused, did I end up on [skeptics.se]? Or is it [history.se] maybe? – Didier L Jan 04 '17 at 16:14
  • @XaolingBao don't ask me. The school lesson also said there was a `friendly` keyword, but I know that doesn't exist for sure. (I think it exists in C++ though) –  Jan 05 '17 at 12:29

5 Answers5

194

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.

Community
  • 1
  • 1
Andrew Li
  • 55,805
  • 14
  • 125
  • 143
  • I'm sure it's not worth much - but I remember when it happened (I was programming as a kid and was very into this new Java thing for some reason) and while I can't find any of the original sources - I remember things exactly like this when I followed them. – Benjamin Gruenbaum Jan 03 '17 at 20:30
  • `Early on, the Java language allowed for certain combinations of modifiers,` Does that mean there was more than just "Private Protected?" – XaolingBao Jan 04 '17 at 13:30
  • @XaolingBao Well of course, one accessor is like no accesor :) The provided links should clarify your question. – m0skit0 Jan 04 '17 at 18:43
52

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.

m0skit0
  • 25,268
  • 11
  • 79
  • 127
  • so is my link an older version of the same content? :D –  Jan 02 '17 at 18:25
  • Maybe the missing information has something to do with that note you put. –  Jan 02 '17 at 18:33
  • @AndrewLi Nowhere neither is stated as *stable* on the given references. And it is definitely confusing to refer to 1.0.2 as 1.0 when there's an actual 1.0. – m0skit0 Jan 03 '17 at 18:11
10

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

TheWaterProgrammer
  • 7,055
  • 12
  • 70
  • 159
  • My link is for method access. Not member access. –  Jan 02 '17 at 18:28
  • 1
    @MarkYisri the same can be used for member variables as well. access specifiers do not work only on methods but also on member vars. In other words access specifiers are a encapsulation concepts & are irrespective of whether you apply it on member methods or member variables. that applies to almost all object oriented languages including C++ & java – TheWaterProgrammer Jan 02 '17 at 18:29
  • Okay, but the tutorial (interestingly) does not mention private protected on variables. Hold on and let me see if there is a variables page... –  Jan 02 '17 at 18:30
  • 1
    you are correct ! Here: https://www.cs.princeton.edu/courses/archive/spr96/cs333/java/tutorial/java/javaOO/accesscontrol.html –  Jan 02 '17 at 18:30
0

No, you can't use both private a protected together. Your tutorial is strange. What you do have is so called package private or in ot6 references package protected access. This is default access that is enabled when no acc6 qualifier is written explicitly.

AlexR
  • 114,158
  • 16
  • 130
  • 208
  • 3
    I was aware you can't use it. I want to know what happened to it, which the other answers explain better. –  Jan 02 '17 at 18:28
  • 4
    Well, the link is from 1996 so given that Java development had just started about a year earlier, the content of the link isn't really that strange :D – Keiwan Jan 02 '17 at 18:31
  • 6
    Good point about the date of theach document. I answered the question while myou train was arriving and wrote it using phone, so sorry if the answer was not derailed enough. Just wanted to help... – AlexR Jan 02 '17 at 21:44
  • 6
    @AlexR your spelling mistake **derailed** is actually a pun (train). Just noticed. :D –  Jan 03 '17 at 00:01
  • 1
    @MarkYisri, detailed. Writing using phone is not the best way to post answers on SO. – AlexR Jan 03 '17 at 13:35
-2

Private scope is withing the existing class. Wherein Protected can be access within package and class extended by classes in other packages.

Seamlessly if you want your variable/methods to be access outside the package you need to define as protected/public otherwise private or some other access specifiers.

Protected methods are usually accessible from outside package and within sub-classes, i.e a class has to extend respective class to avail protected defined methods.

Private methods/variables have scope within the class.They cant be accessible outside the class.

Hence you can't define Private Protected at a same time!

  • This didn't answer the question. I asked why it didn't work. The other answers do a much better job of answering the question. –  Jan 29 '17 at 20:03
  • To clarify further, I know it no longer works now, but the other answers explain why and what happened in the past. Yours doesn't. –  Sep 22 '17 at 11:02