File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes Protected Variables Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Protected Variables" Watch "Protected Variables" New topic
Author

Protected Variables

v bobby
Greenhorn

Joined: Feb 15, 2010
Posts: 7
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

Joined: Jan 28, 2003
Posts: 3954
    
  17

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


Steve
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Protected Variables
 
Similar Threads
Subclass Inheritence for protected variable
Accessing Private Static Variables
Marcus Green's exam #2
protected variables
inherited protected member