Re: Question on member accessibility
- From: ankur <ankur.a.agarwal@xxxxxxxxx>
- Date: Sat, 9 Aug 2008 17:56:13 -0700 (PDT)
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.
.
- Follow-Ups:
- Re: Question on member accessibility
- From: Peter Duniho
- Re: Question on member accessibility
- References:
- Question on member accessibility
- From: ankur
- Re: Question on member accessibility
- From: Peter Duniho
- Question on member accessibility
- Prev by Date: Re: Question on member accessibility
- Next by Date: Re: How to limit the number of web pages downloaded from a site?
- Previous by thread: Re: Question on member accessibility
- Next by thread: Re: Question on member accessibility
- Index(es):
Relevant Pages
|