wood burning stoves 2.0*
The moose likes Java in General and the fly likes 'protected' access control Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark " Watch " New topic
Author

'protected' access control

Wout Er
Greenhorn

Joined: Nov 26, 2012
Posts: 15
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

Joined: Jan 03, 2004
Posts: 6109
    
    6

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
Sheriff

Joined: Sep 28, 2004
Posts: 18532
    
  40

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


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: 'protected' access control
 
Similar Threads
private and protected class - why not?
why protected modifier is not allowed?
Section 6.6.7 of JLS ?
need help from JQ+
Protected Scope