Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

protected access

 
Ed Connery
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
at.protectedField is accessing the protectedField from outside the TestAccess class, so you can't access it. Compare your source to this :
 
Ed Connery
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 73
BSD C++ Fedora
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ed Connery
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1051
Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Shanky Sohar
Ranch Hand
Posts: 1051
Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
see below code

 
Shanky Sohar
Ranch Hand
Posts: 1051
Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1051
Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are Welcome
 
Vishal Kashyap
Ranch Hand
Posts: 73
BSD C++ Fedora
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Shanky and Connery
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic