Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

'protected' access control

 
Wout Er
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

While looking at the member access control modifiers in Java, I did some tests and found something rather unexpected when trying the 'protected' access modifier.

According to Oracle:
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.


This is always true when accessing the protected member either from within the member's class, or from within a non-related class. When attempting to access a protected member from within a subclass, I found that I can access it as described above through either:
- the 'this' reference (implicitly or explicitly)
- the 'super' reference
- a reference variable of the same type as the subclass

However, when using a reference variable of another type (e.g., the member's class type or any of its other subclasses' types), I found that the description above doesn't hold. Instead, in these cases, the member is only accessible from within a class that's in the same package as the protected member's declaring class.

Can anyone explain why?

Thanks in advance!

Best regards,
Wouter
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The purpose of protected is for accessing members defined by our parent as our own members, but not letting them be accessible to the world. What you observed fits with that purpose.
 
Henry Wong
author
Marshal
Pie
Posts: 21498
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Wout Er wrote:
This is always true when accessing the protected member either from within the member's class, or from within a non-related class. When attempting to access a protected member from within a subclass, I found that I can access it as described above through either:
- the 'this' reference (implicitly or explicitly)
- the 'super' reference
- a reference variable of the same type as the subclass

However, when using a reference variable of another type (e.g., the member's class type or any of its other subclasses' types), I found that the description above doesn't hold. Instead, in these cases, the member is only accessible from within a class that's in the same package as the protected member's declaring class.

Can anyone explain why?


See section 6.6.2 of the Java Language Specification...

Java Language Specification wrote:6.6.2. Details on protected Access

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.


Henry
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic