6

I want to invoke a protected method of another instance from within a subclass of the class providing this protected method. See the following example:

public class Nano {

    protected void computeSize() {
    }

}

public class NanoContainer extends Nano {

    protected ArrayList<Nano> children;

}

public class SomeOtherNode extends NanoContainer {

    // {Nano} Overrides

    protected void computeSize() {
        for (Nano child: children) {
            child.computeSize();            // << computeSize() has protected access in nanolay.Nano
        }
    }

}

javac tells me that computeSize() has protected access in Nano. I can't see the reason for this (I thought I was already doing this in some other code). I'd like to keep this method being protected, what can I do?

javac version "1.7.0_09"

Edit

I wanted to provide a stripped down version, but I didn't think about the fact, that the classes lie in different packages.

nanolay.Node
nanolay.NanoContainer
nanogui.SomeOtherNode
Niklas R
  • 16,299
  • 28
  • 108
  • 203

2 Answers2

15

Don't know the rationale, but JLS confirms this in 6.6.2. Details on protected Access (emphasis mine):

A protected member or constructor of an object may be accessed from outside the package in which it is declared only by code that is responsible for the implementation of that object.

So:

package P2;
public class P2 {
    protected void foo() {}
}

.........

package P2A;    
class P2A extends P2.P2 {
    void bar(P2.P2 other) {
        this.foo(); // OK
        other.foo();  // ERROR
    }

    void bar2(P2A other) { 
        other.foo(); //OK
    }
}   

In P2A.bar a call to this.foo() is accessible because this is responsible for implementation of P2 but other.foo() is not accessible because other may not be a P2A. bar2 on the other hand has a P2A so it is all good.

Now, why all is OK if they are all the same package but not if they are different packages? What is the rationale? I don't know and would like to know.

Meta-Comment I have rolled back a recent update by another user as it substantially changes the answer and is probably more suitable as a top level answer itself.

Miserable Variable
  • 28,432
  • 15
  • 72
  • 133
  • Thanks for this summary, I definately agree with you. This behavior does not seem legit to me. – Niklas R Jan 18 '13 at 18:17
  • Perhaps you can ask another question about the rationale> It may not be suitable for this forum, but [programmers](http://programmers.stackexchange.com/questions) likes these types of questions – Miserable Variable Jan 18 '13 at 18:50
  • 1
    why other has to be `P2A` isnt it enough for it to be `P2.P2` to access `foo();` since foo is declared in **P2.P2**. please clarify – amarnath harish Jun 08 '18 at 10:02
  • 1
    *"to accessing protected members or protected methods from outside the same package you cant use the parent class object, only implementing class object can be used to access the protected members of parent class outside same package"* –  Jun 09 '18 at 05:48
  • What if I write like P2 p = new P2A(); p.foo(); Even this is not allowed, I am wondering why? – Chandan Gupta Aug 19 '21 at 06:47
8

You could access the protected methods either by subclassing and overriding; also when they are available in the same package. I will add some details. You can read details here.

The example that you have is on lines of the protected clone() method available in the Object class in java; you cannot directly call it on any object (although all object implicitly extend from the Object class).

Scorpion
  • 3,938
  • 24
  • 37
  • But I cannot subclass and call the method on another instance of the superclass, as I'm doing above? (Assuming they are in different packages, because it works when they are in the same package) – Niklas R Jan 18 '13 at 17:41
  • An instance of someothernode object has access to the protected computeSize method but not of some other instance of someothernode - so not in the for loop. – Scorpion Jan 18 '13 at 17:44
  • @Scorpion that seems incorrect. This compiles without error if they are all in same package. – Miserable Variable Jan 18 '13 at 18:44
  • `clone` cannot be called because it is `protected` in `Object`, which is in package `java.lang` and this class is not in that package. – Miserable Variable Jan 18 '13 at 18:45
  • Yes, it would compile correctly if all the classes are in the same package. The clone example illustrates that - a protected method in a different package cannot be invoked; so you create an employee class outside java.lang, so the clone method on this employee class object cannot be invoked unless it exposes it via public modifier – Scorpion Jan 18 '13 at 18:51