aspose file tools*
The moose likes Beginning Java and the fly likes protected access Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "protected access" Watch "protected access" New topic
Author

protected access

Ed Connery
Ranch Hand

Joined: Mar 11, 2010
Posts: 70
Undoubtedly I'm making an ignorant mistake, but I fail to see why the following does not work. I have a public class AccessTest in a package, and then inherit an object of that class in a public class TestAccess in the default package. When I try to compile TestAccess it finds AccessTest but objects to my attempt to access the protected field and method. I thought that "protected" meant "package plus kids" so that inheritance would allow the access. Here are the two classes.



The specific error message for the field is "TestAccess.java:6: protectedField has protected access in com.edc.AccessTest"
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14687
    
  16

at.protectedField is accessing the protectedField from outside the TestAccess class, so you can't access it. Compare your source to this :


[My Blog]
All roads lead to JavaRanch
Ed Connery
Ranch Hand

Joined: Mar 11, 2010
Posts: 70
Interesting! So I created and tried to print from an AccessTest object when I needed to create a TestAccess object.

That's a mistake worth some thought to change frustrating into fruitful.

Thanks!
Vishal Kashyap
Ranch Hand

Joined: Aug 07, 2010
Posts: 73

Respected Christophe Verré Sir,
Will you please describe it more, I am understanding for way but little bit confused.....
So, Please clarify it more; sir.


MCSA 2003 | Preparing For OCPJP/SCJP6
Ed Connery
Ranch Hand

Joined: Mar 11, 2010
Posts: 70
Vishal Kashyap wrote:
So, Please clarify it more; sir.

Well, let me try so that I can see if I understand. The basic idea is that I created an object of class AccessTest and then attempted to access its protected members from TestAccess, but not from an object of class TestAccess). What I needed was an object of class TestAccess. Since TestAccess extends AccessTest, an object of class TestAccess can <ahem> access a protected member of AccessTest.
Shanky Sohar
Ranch Hand

Joined: Mar 17, 2010
Posts: 1051

protected member if acess outside the package,will be accessed through inheritance only not but making object of superclass.
but if you are access it inside the same package then it could be done.

SCJP6.0,My blog Ranchers from Delhi
Shanky Sohar
Ranch Hand

Joined: Mar 17, 2010
Posts: 1051

see below code

Shanky Sohar
Ranch Hand

Joined: Mar 17, 2010
Posts: 1051

Lets see from JLS....
Note that accessibility is a static property that can be determined at compile time; it depends only on types and declaration modifiers

That means any error related to accessbility will be a compiler catch...i.e compiler error.


For protected member JLS says

A protected member or constructor of an object may be accessed from outside
the package in which it is declared only by code that is responsible for the implementation
of that object.


see this applied to different package only but within the same package there is no restriction it will behave as a default access specifier.
Ed Connery
Ranch Hand

Joined: Mar 11, 2010
Posts: 70
Shanky Sohar wrote:
see this applied to different package only but within the same package there is no restriction it will behave as a default access specifier.

Thanks. And of course if the member had default access then I could only use it from within the package.
Shanky Sohar
Ranch Hand

Joined: Mar 17, 2010
Posts: 1051

You are Welcome
Vishal Kashyap
Ranch Hand

Joined: Aug 07, 2010
Posts: 73

Thanks Shanky and Connery
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: protected access