This week's book giveaway is in the Java 8 forum.
We're giving away four copies of Java 8 in Action and have Raoul-Gabriel Urma, Mario Fusco, and Alan Mycroft 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 Java 8 in Action this week in the Java 8 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: 3775

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: 3056
    
  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: 3775

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: 36598
    
  16
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
How to store method return type in primitive
testing String as immutable
Error in code related to exception
Class level lock versus Object level lock