Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Protected Variables

 
v bobby
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Protected Variables in one class is visible through inheritance to other class which is in different package??

why it is not visible while using object of that class. ??
 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
v bobby wrote:Protected Variables in one class is visible through inheritance to other class which is in different package??

why it is not visible while using object of that class. ??

You will have to show code which shows what problem you are having.

Remember that the variables are only visible in blocks of code which have access to them. So let's say you have this:

This class has a protected member which I want to access elsewhere.

This is a child class in a different package who can see and use the variable just fine.

But if I tried to access the variable from some other class through a NumberDisplayer reference, I can't do that:

On line 9 there I am going to get an access exception. Even though NumberDisplayer has access to the variable, and even though NumberDisplayer and Runner are in the same package, I can't access the variable because it belongs to NumberHolder which is in a different package than Runner and because Runner is not a child of NumberHolder. It is the code which does the accessing (in this case Runner#main()) which needs access to the variable, not necessarily the Object through which the code is calling (the NumberDisplayer).
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic