[3548] in java-interest

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

Re: protected is not?

daemon@ATHENA.MIT.EDU (Michael Daconta)
Wed Nov 15 22:08:06 1995

Date: Wed, 15 Nov 1995 16:14:52 -0700 (MST)
To: Edith Au <edith@pencom.com>
From: daconta@PrimeNet.Com (Michael Daconta)
Cc: java-interest@java.sun.com

At 10:30 AM 11/14/95 -0500, Edith Au wrote:
>Hi,
>
>  protected variables can be accessed by the methods of its class and
subclasses
>
>apiece is an object of junk so apiece can access protected variable name.
>Change name to a private variable if you want name to be accessed only by
>the junk class.
>
>Cheers,
>Edith
>
>> 
>> 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.
>> 
>> Any ideas or is this a bug?
>> 
>> I am using the beta for Win95.
>> 
>>  - Mike Daconta

Hi Edith,

That cannot be correct.  apiece is an instantiation of the junk class; however, 
a protected variable cannot be accessed outside of the junk class or 
its subordinates.  In the example below, the Protect class is able to
get directly at the protected variable in the junk class.  That should 
not compile.

 - 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