SCJP 6
Morteza Manavi-Parast wrote:That's right. A subclass in another package can only access protected members in the superclass via references of its own type or it supertypes. So obviously using a reference type of subclass will solve the problem. Note that the "type of the reference variable" is important and not its actual object type stored in that reference. e.g. this will not work too:
pack p = new test();
Since compiler check the reference type and not the actual object type.
We all know these stuff and it's all true, BUT! What really bugs me in this case is why access to the STATIC PROTECTED FIELD in super class works via a reference type of super class in subclass?? I mean why this works:
System.out.println( p.x2 );
again x2 is protected and p is a reference type of the superclass pack and we run this in the subclass test. So why this works?
Any ideas?
All code in my posts, unless a source is explicitly mentioned, is my own.
SCJP 6
Morteza Manavi-Parast wrote:Thanks and Why we can access a protected member in another class, out of its package, via the super class type?
6.6.2.1 Access to a protected Member
Let C be the class in which a protected member m is declared. Access is permit-
ted only within the body of a subclass S of C. In addition, if Id denotes an
instance field or instance method, then:
• 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 .
• 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 .
All code in my posts, unless a source is explicitly mentioned, is my own.
SCJP 6
Morteza Manavi-Parast wrote:Perfect. Thanks Ruben for clearing my doubt (and also implicitly gave me a definite resource to clear the doubts!) and also thanks Maduranga for classifying the inferred concept in such a nice way!
P.S. There are lots of critical concepts like this that usually missed in SCJP study guide books and I wish this forum had a Wiki so that we could put these concepts in there and make it available for everybody's references. That's unfortunate that soon this point also will be buried and lost under thousand of future posts...
All code in my posts, unless a source is explicitly mentioned, is my own.
Maduranga Liyanage wrote:Yes Morteza..
I studied the K&B (both study guide and Head First) and while they do a really good job in discussing the important points, there are some minute details like this that are missed, and only come across from various other sources...
It would be great to have a WIki for this kind of concepts...
All code in my posts, unless a source is explicitly mentioned, is my own.
SCJP 6
Reading Amazon reviews people have mentioned that they passed SCJP just on the book..
I think it is practically impossible to explain every little thing in Java..
SCJP 6
All code in my posts, unless a source is explicitly mentioned, is my own.
SCJP 6
Morteza Manavi-Parast wrote:Ok, I don't like abstract discussions, so I provide you with an example in the below link (I post it in a separate thread cause it's pretty long and I do not want to messed up here)
FYI - Subclassing from an Inner Class
So, can you please let me know that in your "perspective", is the above link is one of those "Fancy_tricky_concept_only_language_designers_ should_know_about" or is just another crucial point in the exam objectives that someone might be encountered with a real question out of it on the real exam?
Maduranga,
There you go, please read the above thread and let me know if you have any questions.
All code in my posts, unless a source is explicitly mentioned, is my own.
In my "perspective" you can't expect a book to include all possible details. Even the JLS doesn't cover the exam fully. I'm not saying that discussing these details is worthless.
But to put down a book because it doesn't contain all possible details is quite unfair.
... and does a great job at doing what its purpose is, which is getting you prepared for the SCJP exam.
And I must say I still don't get your point. What is your point again?
SCJP 6
All code in my posts, unless a source is explicitly mentioned, is my own.
SCJP 6
All code in my posts, unless a source is explicitly mentioned, is my own.
Did you see how Paul cut 87% off of his electric heat bill with 82 watts of micro heaters? |