This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
Not sure which forum this was suited too so I'm posting it in intermediate, its not an easy question but I don't think my testing agrees with the answers in the study guide I'm reading for the Java 5.0 exam.
Anyway, this is an example question: "True or false: If class Y extends class X, the two classes are in different packages, and class X has a protected method called abby(), then any instance of Y may call the abby() method of any other instance of Y."
Well the book is saying its false: "An object that inherits a protected method from a superclass in a different package may call that method on itself but not on other instances of the same class.".
Well yes, I do realise from other programming (eg. C++) that protected means that subclasses have access to the method. But I disagree with the fact that it cannot be called on other instances of the same class.
I created this code to test it:
And this other class:
My testing is done in a new version of eclipse so it tells me if there are errors before I hit the compile button. There are no errors and it runs as expected printing: "abby from x abby from x"
And this is being run on a new instance of the same class. Does this contradict the question? (the answer would be true...)
These is similar text written in the chapter which says the opposite of what I'm thinking, and I'm confused (am I correct or misinterpreting things).
I have complied with java 5.0 compliance in eclipse (although I'm sure 1.4 is the same).
Thanks, Gareth [ February 27, 2006: Message edited by: Gareth Anderson ]
Joined: Feb 27, 2006
Oh, here's the supporting text for the books answer (the opposite of my answer) from the chapter:
It�s important to be aware that different-package subclasses don�t have unlimited access to protected superclass features. In fact, different-package subclasses have only the following privileges: They may override protected methods of the superclass. An instance may read and write protected fields that it inherits from its superclass. However, the instance may not read or write protected inherited fields of other instances. An instance may call protected methods that it inherits from its superclass. However, the instance may not call protected methods of other instances.
[ February 27, 2006: Message edited by: Gareth Anderson ]
The book is simply wrong. Under no circumstances does Java make any distinction between "this object" and "other objects of this class" when making access control decisions.
There is an interesting subtlety in that, given a class A with a protected method m() and two subclasses B and C in some other package, an instance of B can call m() on itself and other B instances, but not on any instances of C. Just because B inherits m() doesn't give it special access to m() in other classes.