[3524] in java-interest

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

Re: protected is not?

daemon@ATHENA.MIT.EDU (Rajesh Thakkar)
Wed Nov 15 14:49:47 1995

Date: Wed, 15 Nov 1995 11:40:09 -0600
To: java-interest@java.sun.com
From: rajesht@boxer.com (Rajesh Thakkar)

>
>On Mon, 13 Nov 1995, Michael Daconta wrote:
>
>> Here is a simple chunk of code that does not
>> seem to work properly.
>> 
>> class junk {
>>         protected String name;
>> 
>>         junk(String inName)
>>         {
>>                 name = new String(inName);
>>         }
>> }
>> 
>> class Protect {
>>         public static void main(String args[])
>>         {
>>                 junk apiece = new junk("piece of junk");
>> 
>>                 System.out.println(apiece.name);
>>         }
>> }
>> 
>> 
>> This code compiles and prints out the name of the
>> object.  The problem is that the compiler should not have
>> let me do this.  name is a protected variable. protected should
>> mean that inaccesible except to subclasses.
>
>Protected members are avaiable to subclasses and classes in the same *package*
>or compilation unit.  If you put junk in a seperate package its protected 
>members will not be accessible by Protect.  
>
>I believe that this is the default permission if you don't use any modifiers.
>
>
>Pat

Having more than one class defined in a single 
package/compilation unit/module/....... makes them to be "friends"(C++)
automatically. So as suggested by Pat, if junk is compiled in a separate
package and then imported into the package where protect is defined, then
the access to 'apeice.name' from 'portect' class will fail.

Rajesh.

-
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