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.
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.
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..."
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..."
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.
Joined: Mar 01, 2005
I was refferring to the example with private field in parent class, in that case it is impossible.
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 )
Spot false dilemmas now, ask me how!
(If you're not on the edge, you're taking up too much room.)