0

I am blocking on this question : Is every instance of Y able to call the method aMethod() of every other instance of Y. The answer I found is NO.

    package a; 
    class X{ 
    protected void aMethod()
    { } 
    } 

    package b; 
class Y extends X
    { }

Can you explain to me why? thanks.

user199
  • 107
  • 10
  • 1
    See [Controlling Access to Members of a Class](https://docs.oracle.com/javase/tutorial/java/javaOO/accesscontrol.html). The second table on that page explains your question. Where did you find that the answer is "no", and was there any explanation given? – Jesper May 28 '15 at 08:02
  • @Jesper : in an online quizz, but there was no explanation, that's why I asked the question here.. – user199 May 28 '15 at 08:08
  • @Jesper : is No the wrong answer? – user199 May 28 '15 at 08:14
  • @user199 , The answer is perfectly fine i.e. **NO** , reason is protected members can be accessed from the Subclasses in another package . but the Object of Y can be created in other classes also that is why answer is fine . – Neeraj Jain May 28 '15 at 08:15
  • Public variables – Visible to all classes. Protected variables – Visible only to these classes which they belong, and any subclasses. Friendly variables, the default – No modifiers are needed, Visible to the package. Private variables – Visible only to the class to which they belong. – Sasikumar Murugesan May 28 '15 at 08:22
  • @Jesper: I didn't really get what you meant. I am getting confused. Can you please write a counter example as an answer and I'll accept it as the answer. thanks! – user199 May 28 '15 at 08:27

0 Answers0