Re: Question on member accessibility



On Aug 9, 5:05 pm, "Peter Duniho" <NpOeStPe...@xxxxxxxxxxxxxxxx>
wrote:
On Sat, 09 Aug 2008 16:44:59 -0700, ankur <ankur.a.agar...@xxxxxxxxx>  
wrote:

[...]
Why (4) does not work ? I could not understand ( appreciate) the
reason why this access is denied !

When you ask "why", are you simply asking for the rule?  Or why the rule  
exists?

The rule is simple: the "protected" access allows code in a sub-class to  
access base class members, but only via an instance of the sub-class from  
within which the member is being accessed (that is, from a known instance  
of that actual sub-class).  The variable "varA" is not known to be an  
instance of the sub-class "SubB" (and in fact in this case doesn't refer  
to one), and so code within "SubB" does not have access to protected  
members referenced by that variable.

As for why the rule exists, that gets a bit more speculative on my part,  
since I'm not the one who designed Java or similar languages.  However, I  
think it's a reasonable rule, since the "protected" access is there to  
allow sub-classes better control over _themselves_, not other classes.  
The class "SubB" really should only have permission to touch "protected"  
members for instances of itself.  The alternative does not provide enough  
protection for sub-classes that are otherwise unrelated to "SubB".

For example, imagine how disruptive you could be if you could write a  
JComponent sub-class that could retrieve a Graphics instance used to paint  
_some other_ JComponent sub-class by calling the protected method  
getComponentGraphics().  That method is intended for an instance to use  
internally to get its own Graphics instance, not some other instance's.

Or consider (again in JComponent) the protected method  
requestFocusInWindow().  Again, intended for use for a JComponent instance  
on _its own_ behalf.  Other code could be disruptive by calling some other  
instance's requestFocusInWindow() method (especially if that other  
instance was a sub-class that wasn't even supposed to have the focus).

In fact, people could wind up writing sub-classes of certain classes for  
the sole purpose of messing around with _other_ sub-classes of those  
classes.  That breaks encapsulation in some very important ways, and so  
it's not allowed.

Pete


I think the statement in your second para can be modified to say that
" "protected" access allows code in a sub-class ( in some other
package) to access..." because if the subclass is in the same package
then the protected members of superclass can be accessed via
superclass reference which refers to a superclass object.
.



Relevant Pages

  • Re: Question on member accessibility
    ... reason why this access is denied! ... members referenced by that variable. ... protection for sub-classes that are otherwise unrelated to "SubB". ... JComponent sub-class that could retrieve a Graphics instance used to paint   ...
    (comp.lang.java.programmer)
  • Re: Question on member accessibility
    ... reason why this access is denied! ... members referenced by that variable. ... protection for sub-classes that are otherwise unrelated to "SubB". ... JComponent sub-class that could retrieve a Graphics instance used to paint   ...
    (comp.lang.java.programmer)
  • Re: Protected static members, abstract classes, object composition vs. subclassing
    ... Protected is public to some objects and private to others. ... > to public object members. ... Or not, in ECMAScript, where no - caller - property is defined. ... > protection as I said in the original post. ...
    (comp.lang.javascript)
  • Re: adminCount schema attribute
    ... The term "protection" means that the group and its members are protected from changes when delegation has been configured on some OU that contains the group and/or its members. ... BLOG --> http://blogs.dirteam.com/blogs/jorge/default.aspx ... * This posting is provided "AS IS" with no warranties and confers no rights! ...
    (microsoft.public.windows.server.active_directory)
  • Re: News Release
    ... members of militias or volunteer corps forming part of such armed forces. ... customs of war. ... The present Convention shall apply to the persons referred to in Article ... the protection of the present Convention until such time as their status ...
    (comp.security.firewalls)