This week's book giveaway is in the Android forum.
We're giving away four copies of Head First Android and have Dawn & David Griffiths on-line!
See this thread for details.
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 Head First Android 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: 3886
    
    5

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: 2904
    
    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: 3151
    
  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: 3886
    
    5

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: 43287
    
  32
Yes, he is using two packages But it would have been nice to see imports which would not throw compiler errors.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Protected Trouble
 
It's not a secret anymore!