I'm reading Sybex Complete Java 2 Certification Study Guide April 2005 (ISBN0782144195). This book is for java developers who wants to pass java certification.
After a chapter about access modifiers (along with other modifiers) I found the following question (#17):
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.
This question confused me a little.
As far as I know you can call protected method on any variable of the same class (or subclasses). You cannot call it on variables, that higher in the hierarchy than you (e.g. interfaces that you implement).
For example, you cannot clone any object just because you inherit it.
But the questions says nothing about variable type, only about instance type.
I was confused a little and answered "true".
The answer in the book is
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.
There is nothing here about variable type, only about instance type.
This is very strange, I do not understand it.
Can anybody explain what is going on here?
That question seems badly worded - and asks about a very rare edge case (that I'm not even sure is covered on the SCJP test). The way that it's worded makes your answer correct and the given answer incorrect. Coding a similar construct and running it easily proves this...
Let's depict it.
Class X:
Class Y:
Testcase:
Now reread the question: can
y1
callabby()
ony2
,y3
, etc? Will callingabby()
ony1
also call those ofy2
,y3
, etc?For future questions, try to grab pen and paper and interpret the questions literally. There are pretty much holes in those kind of mock questions.
Let's write that down, as BalusC did, and add to Y a method which calls the abby() of any other instance of Y:
It is possible for Y to call the abby() method of any instance of Y to which it has a reference. So the answer in the book is blatantly wrong. Java does not have instance-specific scopes (unlike for example Scala which has an instance-private scope).
If we try to be merciful, maybe the question meant by saying "any other instance of Y" that can it access the method of any instance of Y which happens to be in memory - which is not possible, because Java does not have direct memory access. But in that case the question is so badly worded, that you could even answer: "False. You can not call methods on instances which are on a different JVM, or instances which have been garbage collected, or instances on a JVM which died one year ago etc."
I am almost certain that the question meant:
"any instance of Y may call the abbey() method of any other instance of X" (not Y).
In that case it will indeed fail. To borrow the example from another answer above, the following:
will fail to compile.
The Java Language Specification explains why here: http://java.sun.com/docs/books/jls/third_edition/html/names.html#6.6.2
From The Java Language Specification:
6.6.2.1 Access to a protected Member
Let C be the class in which a protected member m is declared. Access is permitted only within the body of a subclass S of C. In addition, if Id denotes an instance field or instance method, then:
So the protected member is accessible in all instances of S, and the answer in your book is just wrong.
Because variable type is irrelevant here till it is 'sane' in context of question. As method
abby()
belongs toX
(andY
inherits it), it doesn't matter with what type variable referencing instance of Y is declared: it can be eitherX
orY
. Beabby()
accessible, we could call it through both variables: