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

Why does 'protected' allow different classes in the package to access each other's protected members

 
Ranch Foreman
Posts: 626
3
  • Number of slices to send:
    Optional 'thank-you' note:
I saw a question on Enthuware asking about protected access within the same package,
and was very surprised when I tried to test this out.

Can classes within the same package access each others protected variables?
The answer seems to be Yes.
Why does 'protected' allow different classes in the package to access each other's protected members?
Shouldn't it not allow this access to have a progression in the encapsulation and information-hiding?
I understand that 'package' prevents classes outside the package from accessing.



Output:

another class in the package can see my protecteds 10



 
Marshal
Posts: 4533
572
  • Number of slices to send:
    Optional 'thank-you' note:
That topic was discussed previously in this thread.
 
Anil Philip
Ranch Foreman
Posts: 626
3
  • Number of slices to send:
    Optional 'thank-you' note:

Ron McLeod wrote:That topic was discussed previously in this thread.



This question is different from the thread you referenced because here we are considering different classes - not different instances of the same class (which is what the other thread discussed).
 
Anil Philip
Ranch Foreman
Posts: 626
3
  • Number of slices to send:
    Optional 'thank-you' note:

Anil Philip wrote:

Ron McLeod wrote:That topic was discussed previously in this thread.



This question is different from the thread you referenced because here we are considering different classes - not different instances of the same class (which is what the other thread discussed).



I am unable to edit my thread to make a correction
 
Marshal
Posts: 28262
95
  • Number of slices to send:
    Optional 'thank-you' note:

Anil Philip wrote:I am unable to edit my thread to make a correction



Indeed. But would it not be possible for you to reply to this thread with a new post clarifying what you meant to ask?
 
Ron McLeod
Marshal
Posts: 4533
572
  • Number of slices to send:
    Optional 'thank-you' note:

Anil Philip wrote:This question is different from the thread you referenced because here we are considering different classes - not different instances of the same class (which is what the other thread discussed).


Yes - the question is a bit different, but the answer is the same (if I correctly understand what you are asking).

From Controlling Access to Members of a Class:

The Oracle tutorial wrote:The protected modifier specifies that the member can only be accessed within its own package (as with package-private) and, in addition, by a subclass of its class in another package.

 
Anil Philip
Ranch Foreman
Posts: 626
3
  • Number of slices to send:
    Optional 'thank-you' note:

Paul Clapham wrote:

Anil Philip wrote:I am unable to edit my thread to make a correction



Indeed. But would it not be possible for you to reply to this thread with a new post clarifying what you meant to ask?



I used to be able to edit my thread after posting until someone else replied. I found that I am no longer able to do it even immediately after posting.
It appears that this has been revoked for me.
 
Master Rancher
Posts: 4913
74
  • Number of slices to send:
    Optional 'thank-you' note:
Well, here the question may be more about "why is the rule this way?" rather than "what is the rule?".  They could have done things differently, I suppose.  But the way it's written now, we can rank the four levels of access from most accessible to least accessible:

public
protected
package
private

Here, each level includes all the powers of the levels below it.  Between package and protected, it's easy to say which is "more accessible" (protected) and which is "less accessible" (package).

But, if they had made it so protected allowed access from subclasses, but not access from within the same package (other than within a subclass)... in that case, it would not be possible to say one was more or less accessible than another.  Within the same package, package access would be more accessible than protected.  And outside the package, protected would be more accessible than package.  

Why does this matter?   Well to me, first, because I find the nice orderly progression of the first list easy to understand.  And more importantly, I think that would be easier for beginners to understand.  But also, consider the rules for overriding a method.  As it is, we can say that the overriding may be more accessible, but not less accessible.  If we didn't allow protected access to include everything in the same package... then the rules for overriding would get more complicated if you were going from package to protected, or protected to package.
 
Anil Philip
Ranch Foreman
Posts: 626
3
  • Number of slices to send:
    Optional 'thank-you' note:

Mike Simmons wrote:Well, here the question may be more about "why is the rule this way?" rather than "what is the rule?".  They could have done things differently, I suppose.  But the way it's written now, we can rank the four levels of access from most accessible to least accessible:

public
protected
package
private

Here, each level includes all the powers of the levels below it.  Between package and protected, it's easy to say which is "more accessible" (protected) and which is "less accessible" (package).

But, if they had made it so protected allowed access from subclasses, but not access from within the same package (other than within a subclass)... in that case, it would not be possible to say one was more or less accessible than another.  Within the same package, package access would be more accessible than protected.  And outside the package, protected would be more accessible than package.  

Why does this matter?   Well to me, first, because I find the nice orderly progression of the first list easy to understand.  And more importantly, I think that would be easier for beginners to understand.  But also, consider the rules for overriding a method.  As it is, we can say that the overriding may be more accessible, but not less accessible.  If we didn't allow protected access to include everything in the same package... then the rules for overriding would get more complicated if you were going from package to protected, or protected to package.



I wonder if you may have misunderstood the question.
 
Anil Philip
Ranch Foreman
Posts: 626
3
  • Number of slices to send:
    Optional 'thank-you' note:

Anil Philip wrote:

Mike Simmons wrote:Well, here the question may be more about "why is the rule this way?" rather than "what is the rule?".  They could have done things differently, I suppose.  But the way it's written now, we can rank the four levels of access from most accessible to least accessible:

public
protected
package
private

Here, each level includes all the powers of the levels below it.  Between package and protected, it's easy to say which is "more accessible" (protected) and which is "less accessible" (package).

But, if they had made it so protected allowed access from subclasses, but not access from within the same package (other than within a subclass)... in that case, it would not be possible to say one was more or less accessible than another.  Within the same package, package access would be more accessible than protected.  And outside the package, protected would be more accessible than package.  

Why does this matter?   Well to me, first, because I find the nice orderly progression of the first list easy to understand.  And more importantly, I think that would be easier for beginners to understand.  But also, consider the rules for overriding a method.  As it is, we can say that the overriding may be more accessible, but not less accessible.  If we didn't allow protected access to include everything in the same package... then the rules for overriding would get more complicated if you were going from package to protected, or protected to package.



I wonder if you may have misunderstood the question.



I am still not allowed to edit my question.
I find it troubling that the moderator has removed my edit access just because he felt like it.
I don't feel like asking questions on Javaranch any more.
 
Mike Simmons
Master Rancher
Posts: 4913
74
  • Number of slices to send:
    Optional 'thank-you' note:

Anil Philip wrote:I wonder if you may have misunderstood the question.


I may have.  But still, no further clue has been offered.  I understand that it's annoying that you can't edit, and I don't know why that is.  But in a normal linear conversation, I would want to respond to questions after they have been asked, rather than by going back in time to change an earlier post. You could still do that here.  I don't normally go back and re-read prior posts that I've already read, just in case they have changed.
 
Paul Clapham
Marshal
Posts: 28262
95
  • Number of slices to send:
    Optional 'thank-you' note:
Just to add to those comments about editing: Other people may come by later and be confused by answers to questions which weren't asked, if people go back to edit their earlier posts. So we try to avoid that. We regularly ask people not to do it, and when asked, they usually understand. We may also revert edits if they would result in that kind of confusion. Spelling corrections aren't usually a problem but changing an old post in a significant way is not so good.

Personally if I found that a question that I posted on a forum had been misunderstood, I would add a new post to the thread, saying "Oops, I didn't make that clear, here's what I wanted to say..." or words to that effect.
 
Anil Philip
Ranch Foreman
Posts: 626
3
  • Number of slices to send:
    Optional 'thank-you' note:

Paul Clapham wrote:Just to add to those comments about editing: Other people may come by later and be confused by answers to questions which weren't asked, if people go back to edit their earlier posts. So we try to avoid that. We regularly ask people not to do it, and when asked, they usually understand. We may also revert edits if they would result in that kind of confusion. Spelling corrections aren't usually a problem but changing an old post in a significant way is not so good.

Personally if I found that a question that I posted on a forum had been misunderstood, I would add a new post to the thread, saying "Oops, I didn't make that clear, here's what I wanted to say..." or words to that effect.



@Paul,
As mentioned above,

I used to be able to edit my thread after posting until someone else replied.
I found that I am no longer able to do it even immediately after posting.
It appears that this has been revoked for me.



After someone replies, I understand completely that I cannot edit my post.
But as soon as I post and re-read my post and feel I should have phrased things a little differently, or if I left out an important detail,
then I should be able to edit that like I used to - if no one has yet replied.
Did you remove this edit capability for me - and only for me?
 
Paul Clapham
Marshal
Posts: 28262
95
  • 1
  • Number of slices to send:
    Optional 'thank-you' note:

Anil Philip wrote:Did you remove this edit capability for me - and only for me?



First, let me remind you that there are several moderators here. So I'll interpret "you" as "the staff". And we don't get together for meetings to discuss routine actions, we just do them. Restricting editing is a thing which is done occasionally for people who tend to misuse the editing facility, but it is a routine thing.

Let me also point out that if you post something, then decide to edit your post, you don't actually know whether there are replies yet or not. Not unless you refreshed the screen to check that. So it's possible to be mistaken when you think there are no replies.

And earlier in this thread you effectively demanded to be allowed to edit an earlier post, when it already had several replies. By now you should realize that we consider that abusive. Please bear that in mind in future.
 
Anil Philip
Ranch Foreman
Posts: 626
3
  • Number of slices to send:
    Optional 'thank-you' note:

Paul Clapham wrote:
And earlier in this thread you effectively demanded to be allowed to edit an earlier post, when it already had several replies. By now you should realize that we consider that abusive. Please bear that in mind in future.



I think you misunderstood.
If you are referring to this post, http://www.pmsas.pr.gov.br/wp-content/?id=coderanch-1z0-809&exam=p/3563039
I was not asking to edit the original post. (I would not ask to do that since there were replies from others).
I was trying to edit my latest post, to which there were no replies yet.
I found that I was unable to do so and hence the comment.
 
Anil Philip
Ranch Foreman
Posts: 626
3
  • Number of slices to send:
    Optional 'thank-you' note:

Paul Clapham wrote:
And earlier in this thread you effectively demanded to be allowed to edit an earlier post, when it already had several replies. By now you should realize that we consider that abusive.



Paul,
Can you please point out where I "

effectively demanded

to be allowed to edit an earlier post when it already had several replies"?
"we consider that

abusive

".

Please be mindful of your choice of words.
It is wrong to unfairly accuse someone and use terms like "abusive" to characterize them.
I feel I have been slandered.

 
Marshal
Posts: 8880
638
  • 1
  • Number of slices to send:
    Optional 'thank-you' note:
I suggest we get back to the main question or we may just need to close the thread as it no longer fits the purpose.

We have some rules and standards that we try to ensure they are respected and adhered. We do not target individuals, but rather posts, which just appear to be posted by individuals.

So once again, to keep things intact, let's continue on main topic if there is still a wish (in which case I'd remove those posts that are out of context), alternatively, I can close the thread.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
    Bookmark Topic Watch Topic
  • New Topic
vceplus-200-125    | boson-200-125    | training-cissp    | actualtests-cissp    | techexams-cissp    | gratisexams-300-075    | pearsonitcertification-210-260    | examsboost-210-260    | examsforall-210-260    | dumps4free-210-260    | reddit-210-260    | cisexams-352-001    | itexamfox-352-001    | passguaranteed-352-001    | passeasily-352-001    | freeccnastudyguide-200-120    | gocertify-200-120    | passcerty-200-120    | certifyguide-70-980    | dumpscollection-70-980    | examcollection-70-534    | cbtnuggets-210-065    | examfiles-400-051    | passitdump-400-051    | pearsonitcertification-70-462    | anderseide-70-347    | thomas-70-533    | research-1V0-605    | topix-102-400    | certdepot-EX200    | pearsonit-640-916    | itproguru-70-533    | reddit-100-105    | channel9-70-346    | anderseide-70-346    | theiia-IIA-CIA-PART3    | certificationHP-hp0-s41    | pearsonitcertification-640-916    | anderMicrosoft-70-534    | cathMicrosoft-70-462    | examcollection-cca-500    | techexams-gcih    | mslearn-70-346    | measureup-70-486    | pass4sure-hp0-s41    | iiba-640-916    | itsecurity-sscp    | cbtnuggets-300-320    | blogged-70-486    | pass4sure-IIA-CIA-PART1    | cbtnuggets-100-101    | developerhandbook-70-486    | lpicisco-101    | mylearn-1V0-605    | tomsitpro-cism    | gnosis-101    | channel9Mic-70-534    | ipass-IIA-CIA-PART1    | forcerts-70-417    | tests-sy0-401    | ipasstheciaexam-IIA-CIA-PART3    | mostcisco-300-135    | buildazure-70-533    | cloudera-cca-500    | pdf4cert-2v0-621    | f5cisco-101    | gocertify-1z0-062    | quora-640-916    | micrcosoft-70-480    | brain2pass-70-417    | examcompass-sy0-401    | global-EX200    | iassc-ICGB    | vceplus-300-115    | quizlet-810-403    | cbtnuggets-70-697    | educationOracle-1Z0-434    | channel9-70-534    | officialcerts-400-051    | examsboost-IIA-CIA-PART1    | networktut-300-135    | teststarter-300-206    | pluralsight-70-486    | coding-70-486    | freeccna-100-101    | digitaltut-300-101    | iiba-CBAP    | virtuallymikebrown-640-916    | isaca-cism    | whizlabs-pmp    | techexams-70-980    | ciscopress-300-115    | techtarget-cism    | pearsonitcertification-300-070    | testking-2v0-621    | isacaNew-cism    | simplilearn-pmi-rmp    | simplilearn-pmp    | educationOracle-1z0-809    | education-1z0-809    | teachertube-1Z0-434    | villanovau-CBAP    | quora-300-206    | certifyguide-300-208    | cbtnuggets-100-105    | flydumps-70-417    | gratisexams-1V0-605    | ituonline-1z0-062    | techexams-cas-002    | simplilearn-70-534    | pluralsight-70-697    | theiia-IIA-CIA-PART1    | itexamtips-400-051    | pearsonitcertification-EX200    | pluralsight-70-480    | learn-hp0-s42    | giac-gpen    | mindhub-102-400    | coursesmsu-CBAP    | examsforall-2v0-621    | developerhandbook-70-487    | root-EX200    | coderanch-1z0-809    | getfreedumps-1z0-062    | comptia-cas-002    | quora-1z0-809    | boson-300-135    | killtest-2v0-621    | learncia-IIA-CIA-PART3    | computer-gcih    | universitycloudera-cca-500    | itexamrun-70-410    | certificationHPv2-hp0-s41    | certskills-100-105    | skipitnow-70-417    | gocertify-sy0-401    | prep4sure-70-417    | simplilearn-cisa    |
http://www.pmsas.pr.gov.br/wp-content/    | http://www.pmsas.pr.gov.br/wp-content/    |