Meaningless Drivel is fun!*
The moose likes Beginning Java and the fly likes Protected Trouble Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Protected Trouble" Watch "Protected Trouble" New topic
Author

Protected Trouble

Arun Giridharan
Ranch Hand

Joined: Sep 30, 2010
Posts: 290

Hello!

Protected Access Specifier can used in another package if it gets extended right,so why can't i access in the following code.
[Please tell the reason(instead saying it's java rule)]


Thank you
Vijitha Kumara
Bartender

Joined: Mar 24, 2008
Posts: 3817

Your example itself gives the reason. Members marked as protected are only inherited to the sub classes. You cannot use an instance of that class to access a protected member anywhere.


SCJP 5 | SCWCD 5
[How to ask questions] [Twitter]
Sagar Rohankar
Ranch Hand

Joined: Feb 19, 2008
Posts: 2902
    
    1


If this is possible. then what would be the difference between public and protected then ?


[LEARNING bLOG] | [Freelance Web Designer] | [and "Rohan" is part of my surname]
Mohamed Sanaulla
Saloon Keeper

Joined: Sep 08, 2007
Posts: 3068
    
  33

Vijitha Kumara wrote:Your example itself gives the reason. Members marked as protected are only inherited to the sub classes. You cannot use an instance of that class to access a protected member anywhere.


Protected members are accessible within the same package.


Mohamed Sanaulla | My Blog
Vijitha Kumara
Bartender

Joined: Mar 24, 2008
Posts: 3817

Protected members are accessible within the same package

Of course, my statement appeared that it isn't though the OP was referring to two different packages.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38033
    
  22
Yes, he is using two packages But it would have been nice to see imports which would not throw compiler errors.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Protected Trouble
 
Similar Threads
cannot find symbol
testing String as immutable
How to store method return type in primitive
Class level lock versus Object level lock
Error in code related to exception