Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Help to understand the issue with protected method

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?

like image 606
zeroed Avatar asked Feb 11 '10 13:02

zeroed


People also ask

What does a protected method mean?

A protected method is like a private method in that it can only be invoked from within the implementation of a class or its subclasses. It differs from a private method in that it may be explicitly invoked on any instance of the class, and it is not restricted to implicit invocation on self .

How do I access protected methods?

To access a protected method, inherit a class from this calls and then create and instance of the chold class and then using the instance object, call the protected method.

When should a method be protected?

The best time to create a method under the protected heading is when you want to compare and/or transfer data between objects of the same class, but you don't want that information to be made public.

How do you override a protected method?

Yes, the protected method of a superclass can be overridden by a subclass. If the superclass method is protected, the subclass overridden method can have protected or public (but not default or private) which means the subclass overridden method can not have a weaker access specifier.


1 Answers

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.

"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".

Let's write that down, as BalusC did, and add to Y a method which calls the abby() of any other instance of Y:

package one;
public class X {
    protected void abby() {
    }
}

package other;
import one.X;
public class Y extends X {
    public void callAbbyOf(Y anyOther) {
        anyOther.abby();
    }
}

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."

like image 164
Esko Luontola Avatar answered Sep 24 '22 01:09

Esko Luontola