[3504] in java-interest

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

Re: protected is not?

daemon@ATHENA.MIT.EDU (David Geary)
Tue Nov 14 21:50:37 1995

Date: Tue, 14 Nov 1995 17:24:48 -0700
From: David.Geary@Central.Sun.COM (David Geary)
To: daconta@PrimeNet.Com, edith@pencom.com
Cc: java-interest@java.Eng.Sun.COM

> Hi,
> 
> protected variables can be accessed by the methods of its class and subclasses

  OK.

> 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.

  But it's being accessed in a method that is not in Junk or one of it's
subclasses.  (See your first statement).

  If the (Java) code below does not contain a bug, then protected in Java 
means something quite different than protected in C++.  Consider:

#include "iostream.h"

class junk
{
  protected:  int x;
};

class Protect
{
  doit() { junk aJunk; cerr << aJunk.x; }
}

int main()
{
  Protect p;

  p.doit();
}

rmtc >> CC tst.C
"tst.C", line 10: Error: x is not accessible from Protect::doit().

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 David Geary                         "In the backwater swirling
 geary@rmtc.Central.Sun.COM           there is something that'll never change"
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~


> > 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
> > 
> > -
> > This message was sent to the java-interest mailing list
> > Info: send 'help' to java-interest-request@java.sun.com
> ==============================================================================
> Edith Au                            Tel:    (212) 513 7777
> WWW Specialist                      Email:  edith@pencom.com
> Pencom Systems Incorporated         WWW:    http://www.pencomsi.com/~edith      
> 40 Fulton Street,
> NY, NY 10038
> ===============================================================================
> -
> This message was sent to the java-interest mailing list
> Info: send 'help' to java-interest-request@java.sun.com
> 

-
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