[5573] in java-interest

home help back first fref pref prev next nref lref last post

Protected Variables' "Caveat" - Misrepresented?

daemon@ATHENA.MIT.EDU (Mike Afergan)
Sat Feb 17 18:49:14 1996

From: mikea@ai.mit.edu (Mike Afergan)
To: java-interest@java.sun.com
Date: Sat, 17 Feb 1996 18:22:26 GMT

	I am pretty sure that I understand the implications of the protected
modifier for variables and am thus confused by the concluding
statement in the on-line tutorial.

>Let's introduce a new class, Gamma, that derives from Alpha but lives in a different package. 
>The Gamma class can access the iamprotected  
> variable, but only on objects of type Gamma or its subclasses. For  
> example, the accessMethod() of the following class attempts to access 
> the iamprotected variable on an object of type ProtectedAlpha, which 
> is illegal, and on an object of type Gamma, which is legal. 

>               class Gamma {
>                   void accessMethod(ProtectedAlpha a) {
>                      a.iamprotected = 10;    //illegal
>                       this.iamprotected = 10;
>                   }
>               }

	This makes perfect sense to me.  Because Gamma is not in the same
package as ProtectedAlpha, it cannot access a.imaprotected - protected
variables cannot be accessed outside of their package.  However,
because Gamma is a subclass of Alpha it can access this.imaprotected -
subclasses can access protected variables.
	If this is correct, the final paragraph doesn't make very much sense:

>          If a class is both a subclass of and in the same package as 
>the class with  the protected variable, then the class has access to 
>the protected variable   (its package status takes precedence over its subclass status). 

	The first part says something that seems rather obvious.  Both
subclasses AND members of the same package have access to protected
variables.  Being both shouldn't limit the variables that a given
class can access.
	What it in parenthesis thefore doesn't make sense for two reasons:
	1.  It is illogical as a result of what I said in the last paragraph.
	2.  It contradicts what we saw in the Gamma example.

	In the Gamma example we saw a class who by virtue of being in a
diffrent package did not have access to a.imaprotected.  However,
because it was a subclass of Alpha, the tutorial says that it did have
access to this.imaprotected.  Thus, we saw the subclass taking
precedence over the package, not vice versa.

	Hopefully, I have missed something along the way and someone will be
able to point this out.  Otherwise, it is simply a case of poor
wording on the part of the author?

	Thanks,
	-- Mike


-
This message was sent to the java-interest mailing list
Info: send 'help' to java-interest-request@java.sun.com

home help back first fref pref prev next nref lref last post