aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Section 6.6.7 of 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 "Section 6.6.7 of JLS ?" Watch "Section 6.6.7 of JLS ?" New topic
Author

Section 6.6.7 of JLS ?

Munish Gulati
Ranch Hand

Joined: Aug 06, 2003
Posts: 37
The Code Snippet:-

Answer: It will not compile
Comments:
Although, class B extends class A and 'i' is a protected member of A, B still cannot access i, (now this is imp)
THROUGH A's reference because B is not involved in the implementation of A.
Had the process() method been defined as process(B b); b.i would have been accessible as B is involved in the
implementation of B.
For more information read Section 6.6.7 of JLS:
______________________________________________________________
My Problem:-
What does it mean by "B is not involved in the implementation of A". I have read the section 6.6.7 of JLS but could
not get it.
If anybody can explain me the meaning it would be of great help
Thanks
[ August 17, 2003: Message edited by: Thomas Paul ]

Munish Gulati<br />SCJP 1.4<br />Albert Einstein: There are only two ways to live your life. One is as though nothing is a miracle. The other is as though everything is a miracle.
Jose Botella
Ranch Hand

Joined: Jul 03, 2001
Posts: 2120
from JLS

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.

I would say:
A class is responsible for the implementation of those instances of the same type as itself, and also of its subtypes.

Not ok, class B is not involved in the implementation of an instance of type A.

Both ok. Class B is involved in the implementation of instances of B and C.
[ August 17, 2003: Message edited by: Jose Botella ]

SCJP2. Please Indent your code using UBB Code
Kathy Sierra
Cowgirl and Author
Ranch Hand

Joined: Oct 10, 2002
Posts: 1572
Howdy,
I'm not sure what they were trying to explain, but I can tell you what the problem is...
(and this is perhaps the most commonly misunderstood part of Java access, so pretty much everybody has trouble with this!)
It centers around what the 'protected' modifier really means. You'll often see it in charts and tables like this:
default: accessible only to classes in the same package.
protected: accessible only to classes in the same package or subclasses, even when the subclasses are outside the package.
But that just doesn't explain it!!
Because for 'protected', you really do NOT have full access to a protected member of your superclass, if you're a subclass and outside the package of the superclass.
'protected' means "Access through Inheritance ONLY". In other words, if a member is marked 'protected', subclasses outside the package can access that member ONLY by inheriting it. The subclass "has" the member, but they can't ACCESS the member using an instance of the superclass.

So, class B can't make an instance of A and then access the protected 'i' variable.
But... class B can access this.i. B can get his *own* inherited 'i' variable, but he cannot ask for the 'i' variable of an instance of A, the superclass.
Bottom line: You can't use a superclass reference to get to a protected member of your superclass if you are not in the same package as the superclass. However, you CAN use your own 'this' reference to access the protected member, because you definitely inherited it.
One more note while we're here...
With protected, what happens to the subclass outside the package, when IT has subclasses? Can those subclasses still inherit the protected 'i'? Yes. Once a member is marked 'protected', that member is inherited all the way down regardless of where the other subclasses live (in other words, it makes no difference which packages the subclasses are in). BUT... once you have a subclass outside the package, the *other* classes in the subclass-outside-the-package's package cannot access the protected member. For example, ANY class in A's package can access 'i' -- it acts default within the class in which the member was declared. But within B, nobody in B's package can access 'i', so from that perspective the protected member acts as though it were marked private! Except... it isn't *completely* private, because subclasses of B will still *inherit* the member.
So, 'protected' is a very strange modifier, because it means "access through inheritance only", except for other classes defined within the same package as the class that declared the protected member (in which case, the protected member is just like 'default' access).
Whew!
I probably just made things more confusing
cheers,
Kathy
Munish Gulati
Ranch Hand

Joined: Aug 06, 2003
Posts: 37
I am trying to digest the things you wrote..thanks for quick reply
Priyanka Chopda
Ranch Hand

Joined: Jul 22, 2003
Posts: 112
Thanks Kathy,
I read same point yesterday from book but was not able to get it. Today with this example and your explanation it is clear.
That was good explanation.
-PC
Marlene Miller
Ranch Hand

Joined: Mar 05, 2003
Posts: 1391
I draw pictures to remember what protected means.

An object of class B cannot access protected methods inherited from class A invoked on objects through references of type A or C.
An object of class B can only access protected methods inherited from class A invoked on objects through references of type B and subclasses of B.
[ August 18, 2003: Message edited by: Marlene Miller ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Section 6.6.7 of JLS ?