• Post Reply Bookmark Topic Watch Topic
  • New Topic

Protected with JDK6 update 20  RSS feed

 
Damilola Okuboyejo
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • 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
}
}
 
Avishkar Nikale
Ranch Hand
Posts: 173
  • Mark post as helpful
  • send pies
  • 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.

 
Rob Spoor
Sheriff
Posts: 21131
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • 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
  • Quote
  • Report post to moderator
Am I missing something or this code should have given error even on 1.5 ??

 
Ninad Kuchekar
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
  • 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
 
Campbell Ritchie
Marshal
Posts: 56518
172
  • Mark post as helpful
  • send pies
  • 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
  • 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: 21131
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • 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
  • 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: 21131
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • 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
  • 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.
 
Shanky Sohar
Ranch Hand
Posts: 1051
Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
  • 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: 21131
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
Rob & Shanky,

Thanks for the clarification.
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!