File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Doubt in K&B SCJP 5: protected access Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Doubt in K&B SCJP 5: protected access" Watch "Doubt in K&B SCJP 5: protected access" New topic
Author

Doubt in K&B SCJP 5: protected access

Peter Schubert
Greenhorn

Joined: Sep 15, 2006
Posts: 5
On page 36, the book says:

The bottom line: when a subclass-outside-the-package inherits a protected member, the member is essentially private inside the subclass, such that only the subclass and its subclasses can access it.


However, a neighbor of the base class can still access the protected member in the subclass.

Instead, it should probably say:
...the member retains the protected access, as if in the original package of the base class.
Surendra Kumar
Ranch Hand

Joined: Jul 04, 2006
Posts: 235
what do you mean by neighbor of base class?

However, a neighbor of the base class can still access the protected member in the subclass.
Keith Lynn
Ranch Hand

Joined: Feb 07, 2005
Posts: 2367
I think in that particular statement they were talking about classes and packages outside of the package containing the superclass.
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
There was similar (long) discussion a little way back.


Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
Peter Schubert
Greenhorn

Joined: Sep 15, 2006
Posts: 5
Originally posted by Surendra Nichenametla:
what do you mean by neighbor of base class?



I was refering to a class in the same package as the base class.
Peter Schubert
Greenhorn

Joined: Sep 15, 2006
Posts: 5
Originally posted by Keith Lynn:
I think in that particular statement they were talking about classes and packages outside of the package containing the superclass.


The previous paragraph on the same page starts with
But there's still one more issue we haven't looked at...what does a protected
member look like to other classes trying to use the subclass-outside-the-package to
get to the subclass' inherited protected superclass member?

which does not limit the discussion to classes outside the package of the base class. Even if the example uses a "Neighbor" of the subclass-outside-the-package, the statement I was referring to in my original post is a "bottom line", which tries to generalize the conclusion.
Peter Schubert
Greenhorn

Joined: Sep 15, 2006
Posts: 5
Originally posted by Barry Gaunt:
There was similar (long) discussion a little way back.


Thank you for pointing that out! I tried to find a similar thread before posting this, but I seem to have missed this one.

However, the discussion there focuses on the private-like access for classes in the same package as the subclass, and the distinction between "almost the same effect as private" and "private per se".

My point is that a better analogy could be made with the protected access instead of private. I think it's easier to remember that an inherited protected member still acts as protected, but it also "remembers" the package that "protects" it.

Here's also an example of what I'm trying to say:
Bert Bates
author
Sheriff

Joined: Oct 14, 2002
Posts: 8815
    
    5
Hi all, this is actually Kathy answering the question under Bert's account.

While we're talking about my "bottom line" statement, I think the REAL "bottom line" is that Peter is absolutely right:

"My point is that a better analogy could be made with the protected access instead of private. I think it's easier to remember that an inherited protected member still acts as protected, but it also "remembers" the package that "protects" it."

I oversimplified with a way of thinking about it that supports anything that would ever be asked on the exam, but breaks down where Peter demonstrated. We always struggle with this... we DO sometimes oversimplify and leave off edge cases, following kind of a 95/5 rule... where the [simpler] thing we discuss covers 95% of the topic, and 100% of what you'd get on the exam, where the extra 5% would add way too much cognitive overhead for virtually little or no gain.

Peter's argument, however, is an example of where we didn't get it right...because his way of explaining it is just as simple, but more correct!

Thanks Peter. We'll flag this for our next revision.

Cheers,
Kathy


Spot false dilemmas now, ask me how!
(If you're not on the edge, you're taking up too much room.)
 
Consider Paul's rocket mass heater.
 
subject: Doubt in K&B SCJP 5: protected access