• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

protected access

 
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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"
 
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
Ranch Hand
Posts: 73
BSD C++ Fedora
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 1051
Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
see below code

 
Shanky Sohar
Ranch Hand
Posts: 1051
Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You are Welcome
 
Vishal Kashyap
Ranch Hand
Posts: 73
BSD C++ Fedora
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Shanky and Connery
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic