Hi Ninad,
I think you are absolutely correct. However, I am looking for any reasoning behind it. Here is something I came up with after I submitted the original post: all the non-static member variable in a class can be explictly refered to using "this" keyword, which mean current object reference. That mean
refering to the same variable in my example class B or C. Because "this" is representing a instance reference B or C, I think it makes sense that by using obj2 or obj3, we can access pf. I know the logic may be a little broken. For example, It doesn't explain why you can access pf in Class B using obj3, an reference to an instance of Class C.
Also, I am really gald that you brought up
But if protected member is static then it can be accessible to sub class using super class reference.
From a post in this forum, we
You will get:
test.java:10: x4 has protected access in packageA.pack
System.out.println( p.x4 );
We see that we can use p or pack or even
test to access x2, but an attempt to access x4 using p failed. As you can see, I understand the rule but don't know why.
Thanks
-Cheung
P.S. Of course, the simplest answer will be JVM or the
Java compiler behavior this way
