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
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

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

 
Ranch Hand
Posts: 662
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
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



 
Sheriff
Posts: 4641
582
VSCode Eclipse IDE TypeScript Redhat MicroProfile Quarkus Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
That topic was discussed previously in this thread.
 
Anil Philip
Ranch Hand
Posts: 662
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator

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 Hand
Posts: 662
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator

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
 
Sheriff
Posts: 28323
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator

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
Sheriff
Posts: 4641
582
VSCode Eclipse IDE TypeScript Redhat MicroProfile Quarkus Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator

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 Hand
Posts: 662
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator

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: 5060
81
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
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 Hand
Posts: 662
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator

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 Hand
Posts: 662
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator

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: 5060
81
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator

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
Sheriff
Posts: 28323
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
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 Hand
Posts: 662
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator

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
Sheriff
Posts: 28323
95
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator

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 Hand
Posts: 662
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator

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, https://coderanch.com/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 Hand
Posts: 662
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator

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: 8960
646
Mac OS X Spring VI Editor BSD Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
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.
 
A lot of people cry when they cut onions. The trick is not to form an emotional bond. This tiny ad told me:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
    Bookmark Topic Watch Topic
  • New Topic