4

I'm passing an object reference to a Util class. I'm attempting to invoke a protected method on the Util class but I getting a compile time error -

The method setPositionChild(Field, int, int) from the type Manager is not visible

To invoke a protected method is it required to be in the implementing class only ? Can I not pass the reference to an external class and invoke the reference from there ?

blue-sky
  • 51,962
  • 152
  • 427
  • 752
  • possible duplicate of [In Java, what's the difference between public, default, protected, and private?](http://stackoverflow.com/questions/215497/in-java-whats-the-difference-between-public-default-protected-and-private) – Péter Török Jul 15 '11 at 10:09

1 Answers1

7

Protected method can be accessed from subclasses. Make that method public if you want to access from any class. See details here

Also as mentioned by @Sean Patrick Floyd, from classes in the same package!

Nishant
  • 54,584
  • 13
  • 112
  • 127
  • 2
    And from classes in the same package! – Sean Patrick Floyd Jul 15 '11 at 10:13
  • "Protected method can be accessed from subclasses." should this not be "Protected method can be accessed from subclass implementation" since the compile time error is thrown when I pass the reference of the subclass to an external class and invoke the method from that external class ? – blue-sky Jul 15 '11 at 10:23
  • 1
    @user470184 You can't access a protected method of an instance unless you are trying to access it from (a) somewhere in it's subclass, or (b) in a class that sits in the same package. Having an instance of an object does not allow you to access it's protected method. What I understand from your description is you require to make that method public. – Nishant Jul 15 '11 at 10:29