aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes protected Access in jls Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "protected Access in jls" Watch "protected Access in jls" New topic
Author

protected Access in jls

James Du
Ranch Hand

Joined: Mar 23, 2001
Posts: 186

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.
6.6.2.1 Access to a protected Member
Let C be the class in which a protected member m is declared. Access is permitted only within the body of a subclass S of C. In addition, if Id denotes an instance field or instance method, then:
1.If the access is by a qualified name Q.Id, where Q is an ExpressionName, then the access is permitted if and only if the type of the expression Q is S or a subclass of S.
2.If the access is by a field access expression E.Id, where E is a Primary expression, or by a method invocation expression E.Id(. . .), where E is a Primary expression, then the access is permitted if and only if the type of E is S or a subclass of S.

pls. look at the above quote from jls, i am wondering what's the distinction between the 2 subcases and what restriction the 2 subcases introduced to the protected Access. I dont think the 2 subcases belongs to the additional situation at all, I think they're useless!
Do I miss something?
I need a segment of code for clarification, thanks in advance!
pls. give the quote another minute consideration, It really complexed me alot!
[This message has been edited by James Du (edited April 25, 2001).]
James Du
Ranch Hand

Joined: Mar 23, 2001
Posts: 186
I give a null post for a new positoin and hope some notice
Stevie Kaligis
Ranch Hand

Joined: Feb 04, 2001
Posts: 400
No James...they are not useless,
here is what I've found !!
consider this code :
// File: abc/Q.java
package abc;
public class Q {
protected int protectedVar;
}
and this code is in the default package:
// File: Tester.java
import abc.Q;
public class Tester {
private Q q = new Q() {
void someMethod() {
protectedVar = 1; // will this compile?
}
};
}
see..., class Tester does not extends Q, and both class are in the different package...!
Both classes compile cleanly. You may wonder why the assignment statement seems to compile cleanly since it appears that a protected member is being accessed from within a class (Tester) that does not extend the class that declared the protected member (class Q). Specifically, it may seem strange that class Tester has access to the protected member, protectedVar, of class Q even though Tester is not a subclass of Q and the two classes are in different packages.
In actuality, the reference to protectedVar is not taking place within Tester at all! Note the declaration of the variable q within Tester. q is an instance of a subclass of Q. What's the name of this subclass? Well, it doesn't have a name -- it's an anonymous inner class. The important thing to realize is that since q is an instance of a subclass of Q, it has access to the protected members in Q.
The result of compiling the above code is three separate class files: Q.class, Tester.class, and Tester$1.class. Tester$1 is the name the Java compiler assigned to the anonymous inner class of Q.
hope that helps
stevie
James Du
Ranch Hand

Joined: Mar 23, 2001
Posts: 186
Hi Stevie, thanks very much for your reply.
Well, I still cant quite understand the issue. Let we consider your code

public class Tester
{
private Q q = new Q()
{
void someMethod()
{
protectedVar = 1; // will this compile?
}
}
};

I think the assignment statement still fall into the case below since the annoymous class Q could be considered a subclass of Q.
Access is permitted only within the body of a subclass S of C.

what complexed me is that what restriction the following subcases introduced

In addition, if Id denotes an instance field or instance method, then:
1.If the access is by a qualified name Q.Id, where Q is an ExpressionName, then the access is permitted if and only if the type of the expression Q is S or a subclass of S.
2.If the access is by a field access expression E.Id, where E is a Primary expression, or by a method invocation expression E.Id(. . .), where E is a Primary expression, then the access is permitted if and only if the type of E is S or a subclass of S.

Certainly the access of instance field or instance method is only permitted to the instance of the class or the subclass of which the field or method declared!
I wonder why the situation reiterated here as a additional restriction. Don't you think the specification here is superfluous or doo i miss some subtlety?
Regards.
James
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: protected Access in jls