• 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
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

Protected with JDK6 update 20

 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All,

Really which Bert and Kathy are reading this!!
I noticed in JDK6 u20 d@ protected behaves just like default access (package private). Instance data with protected access can't be accessed outside of their package even NOT by a class instance member that has is-a relationship with the hosting class
Please anyone d@ has worked with JDK6 u20 should verify this

e.g

package here;

public class Utility
{
protected int resource;
}

package one;

import here;

public class Test extends Utility
{
public static void main(String[] argv)
{
Utility ut = new Utility();
ut.resource = 67; // ERROR resource can't be accessed outside of its package
}
}
 
Ranch Hand
Posts: 173
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There is an example on page 35 in SCJP Study Guide by Kathy & Bert which can help you understand this.

 
Sheriff
Posts: 22802
131
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Damilola Okuboyejo wrote:d@


Please UseRealWords. It took me a minute to figure out you meant "that".

I'll have a counter example that does show the difference between protected and default access:

Protected members can only be accessed on instances of the sub class, not on instances of the super class. Because I changed the reference type (and actual type) it started working.
 
Damilola Okuboyejo
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Avishkar Nikale wrote:There is an example on page 35 in SCJP Study Guide by Kathy & Bert which can help you understand this.



Hi Nikale

I actually understand very well the concept of protected.
i noticed d@ observation while working with JDK6 update 20
protected member were restricted to just the package
 
Damilola Okuboyejo
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Rob Prime wrote:

Damilola Okuboyejo wrote:d@


Please UseRealWords. It took me a minute to figure out you meant "that".

I'll have a counter example that does show the difference between protected and default access:

Protected members can only be accessed on instances of the sub class, not on instances of the super class. Because I changed the reference type (and actual type) it started working.




thanks Rob
and sorry about the short-code ''d@''
got d clarification.
Guess this is an improvement in JDK6 update 20, because the ealier code used to work in previous versions, using the super-class reference to access the protected member, since it's the original owner of the resource
 
Avishkar Nikale
Ranch Hand
Posts: 173
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Am I missing something or this code should have given error even on 1.5 ??

 
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Damilola ,

Below is a link in which you will find a mapping table for all the access modifiers, it might clear your doubt.

Java Access Modifiers


-Ninad
 
Marshal
Posts: 79704
381
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Slight confusing point about that table: it says classes can only be public or package-private. In fact that applies to top-level classes; classes inside classes can be private or protected.
 
Ninad Kuchekar
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Campbell,

You are right, however I am confused again...

Protected can be accessed from the same package and a subclass existing in any package, right? Which brings us back to the Damilola's question...


Ninad Kuchekar wrote: Below is a link in which you will find a mapping table for all the access modifiers, it might clear your doubt.



The link has added to the doubt I suppose??
 
Rob Spoor
Sheriff
Posts: 22802
131
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Damilola Okuboyejo wrote:

Rob Prime wrote:

Damilola Okuboyejo wrote:d@


Please UseRealWords. It took me a minute to figure out you meant "that".



thanks Rob
and sorry about the short-code ''d@''
got d clarification.


What makes you think that, if "d@" isn't accepted as a replacement for "that", that "d" would be acceptable as replacement for "the"? You haven't even read the UseRealWords page, have you?
 
Damilola Okuboyejo
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Rob Prime wrote:

Damilola Okuboyejo wrote:

Rob Prime wrote:

Damilola Okuboyejo wrote:d@


Please UseRealWords. It took me a minute to figure out you meant "that".



thanks Rob
and sorry about the short-code ''d@''
got d clarification.


What makes you think that, if "d@" isn't accepted as a replacement for "that", that "d" would be acceptable as replacement for "the"? You haven't even read the UseRealWords page, have you?



Sorry again
CORRECTION HAS BEEN NOTED

did you try the access using the super-class in other version other than JDK6 uipdate 20?
I have seen this work on earlier version but just want to clarify that
 
Rob Spoor
Sheriff
Posts: 22802
131
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As far as I know, it has always worked like this. protected constructors can really only be called as a chained constructor from sub class constructors. protected members can only be accessed by a sub class if they are its own members, like inside other methods of the sub class or if the reference type is the sub class itself.
 
Avishkar Nikale
Ranch Hand
Posts: 173
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Rob Prime wrote:As far as I know, it has always worked like this. protected constructors can really only be called as a chained constructor from sub class constructors. protected members can only be accessed by a sub class if they are its own members, like inside other methods of the sub class or if the reference type is the sub class itself.


Rob,

Should the example provided by Damilola failed in all cases of JDK versions.
 
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
Yes it will fail in all the jdk versions....
because protected member can only be access by inheriting class outside the package


but if you are acessing it inside the same package you can do like that..which is showing in the first post.
 
Rob Spoor
Sheriff
Posts: 22802
131
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Avishkar Nikale wrote:

Rob Prime wrote:As far as I know, it has always worked like this. protected constructors can really only be called as a chained constructor from sub class constructors. protected members can only be accessed by a sub class if they are its own members, like inside other methods of the sub class or if the reference type is the sub class itself.


Rob,

Should the example provided by Damilola failed in all cases of JDK versions.


It should at least fail in all versions over Java 1.4.
 
Avishkar Nikale
Ranch Hand
Posts: 173
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Rob & Shanky,

Thanks for the clarification.
 
Please enjoy this holographic presentation of our apocalyptic dilemma right after this tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic