wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes K&B SCJP 5 forgets to mention 2 cases for protected access Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "K&B SCJP 5 forgets to mention 2 cases for protected access" Watch "K&B SCJP 5 forgets to mention 2 cases for protected access" New topic
Author

K&B SCJP 5 forgets to mention 2 cases for protected access

Saskia de Jong
Ranch Hand

Joined: Jan 24, 2006
Posts: 34
I'm currently reading through K&B SCJP 5. In the explantion of access control they seem to forget 2 important cases for protected members. Namely, using reference access to a protected member on a instance of the same type, or subtype as the calling code.

E.g.




These two cases are surely different from those in the book, yet they are not mentioned. I've been teaching C++ in the past and this issue always caused a great deal of confusion. Of course Java is not C++, but it seems to have the same confusion here. I'm not sure if the SCJP exam asks for those details though.
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
I don't have the K&B book yet, so I'm not sure how it addresses this issue. Notice that you can do this same thing with private members. It seems to me that these issues would fit more under describing how private access works and then some disclaimer like "protected members can be accessed the same way private members can plus..."

Layne


Java API Documentation
The Java Tutorial
Saskia de Jong
Ranch Hand

Joined: Jan 24, 2006
Posts: 34
Originally posted by Layne Lund:
I don't have the K&B book yet, so I'm not sure how it addresses this issue.



Well, is just doesn't. There is an explanation about the differences between inherited access, direct access and reference access. After that some cases are described. On page 36 they write in bold:

For a subclass outside the package, the protected member can be accessed only through inheritance.

A few pages before that, inheritance access was defined as code accessing an inherited member through the this reference (using this either implicit or explicit). Clearly, when accessing a member through the reference of another instance we're not using "this" in anyway.

Therefore, the K&B reading is slightly misleading, or atleast incomplete. But maybe this is mentioned in the book elsewhere. I haven't read all chapters yet.


Notice that you can do this same thing with private members. It seems to me that these issues would fit more under describing how private access works and then some disclaimer like "protected members can be accessed the same way private members can plus..."


You're right. Something like that would do.
levani dvalishvili
Ranch Hand

Joined: Mar 01, 2005
Posts: 99
Originally posted by Layne Lund:
. [b]Notice that you can do this same thing with private members. ..."

Layne


I dont agree and dont understand, how can you do same thing with private members when private members are completly invisible ?
[ January 29, 2006: Message edited by: levani dvalishvili ]

SCJP 1.5(Done) SCJA 1.0(Done)<br />SCWCD(in Progress...)
Saskia de Jong
Ranch Hand

Joined: Jan 24, 2006
Posts: 34
Originally posted by levani dvalishvili:


I dont agree and dont understand, how can you do same thing with private members when private members are completly invisible ?


It's understandable that you don't understand this. It's a little known fact that code can access members of other instances of the same type (or subtype) even though they are private. The following code compiles without problems:



As you can see in this example, the code inside test() accesses the private member of testObj as if it was a public member. This would also work for subclasses of TestClass.
levani dvalishvili
Ranch Hand

Joined: Mar 01, 2005
Posts: 99
I was refferring to the example with private field in parent class, in that case it is impossible.
Bert Bates
author
Sheriff

Joined: Oct 14, 2002
Posts: 8883
    
    5
I'm going to let you guys sort this one out, at which point we'll take a look at whatever argument is left standing

One comment I will make however is that the book is very focused on the exam topics, as opposed to being a complete reference to all the in-and-outs that exist within Java. It could be that we missed something here that's on the exam, again I'll wait to see what conclusion you guys come to, but it's more likely that we skipped this particular detail because it's not on the exam. I'm hedging here because I haven't really studied this thread in detail.

We do pay attention to the posts on this forum, but in general we like for the SCJP candidates (that's you guys), to take the first whack at sorting out any issues that might come up with the book. If, after a discussion, the consensus is that we missed something or that we made an error, we'll make a response at that point. (We'd hate to preempt any good learning opportunites for you guys )

hth,

Bert


Spot false dilemmas now, ask me how!
(If you're not on the edge, you're taking up too much room.)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: K&B SCJP 5 forgets to mention 2 cases for protected access