• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Section 6.6.7 of JLS ?

 
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Cowgirl and Author
Posts: 1589
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am trying to digest the things you wrote..thanks for quick reply
 
Ranch Hand
Posts: 112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 1392
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic