aspose file tools*
The moose likes Beginning Java and the fly likes Access protected member from the class of different package. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Access protected member from the class of different package." Watch "Access protected member from the class of different package." New topic
Author

Access protected member from the class of different package.

Mihir Patel
Greenhorn

Joined: Apr 26, 2011
Posts: 18

Hi,

I have confusion that why can't we access the protected member from the subclass in different package using the parent class reference. i need to know the reason for it.

for example,

In below code why can't we access the protected variable 'x' using the parent class (Parent) reference.




jishnu dasgupta
Ranch Hand

Joined: Mar 11, 2011
Posts: 103

Hi Mihir,

The protected access specifier requires that the protected member is visible in a subclass outside the package only as inherited member of the subclass.
So


works absolutely fine.....

However when you are using the Parent reference, the member varriable x is being accessed as an instance varriable of Parent class and not as a varriable inherited by Child from Parent. So you get the complier error.


If debugging is the process of removing bugs, then programming must be the process of putting them in. -- Edsger Dijkstra

Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39409
    
  28
Welcome to the Ranch The full details are to be found in the Java™ Language Specification. It's not easy reading, however.
Mihir Patel
Greenhorn

Joined: Apr 26, 2011
Posts: 18

Thanks a lot... Jishnu....
Mihir Patel
Greenhorn

Joined: Apr 26, 2011
Posts: 18

Campbell Ritchie wrote:Welcome to the Ranch The full details are to be found in the Java™ Language Specification. It's not easy reading, however.


Thanks Ritchie..
As you said it's not easy to read, but it also really a good stuff to clear my understandin regarding protected member access...

thanks again..
jishnu dasgupta
Ranch Hand

Joined: Mar 11, 2011
Posts: 103

you are welcome..
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39409
    
  28
jishnu dasgupta wrote:you are welcome..
Agree
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Access protected member from the class of different package.