Account Options

  1. Sign in
The old Google Groups will be going away soon.
Switch to the new Google Groups.
Google Groups Home
« Groups Home
Is this a problem with me or java?
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  6 messages - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
Keith Allan Barrett  
View profile  
 More options Apr 22 1997, 3:00 am
Newsgroups: comp.lang.java.programmer
From: Keith Allan Barrett <keith...@wpi.wpi.edu>
Date: 1997/04/22
Subject: Is this a problem with me or java?

I would expect the following program to produce an error on
the fact that objects shouldn't have access to their super
classes private data. It seems in this case that a object
can gain access to its private data if it is accessed from
another object with the same class as its super class. I am
new to this and would appreciate any feedback I can get.

Thanks in Advance,
Keith Barrett

-----------------------------------------------------------

class test {
  public static void main(String args[]) {
    (new Wow1()).test();  

    // This statement is not legal
    // ((new Wow2()).z;  
  }

}

class Wow1 {
  private int z = 7;

  public void test() {
    Wow2 wow2 = new Wow2();

    // Why can I access and modify the private properties of my super class
    System.out.println(wow2.z);
    wow2.z = 4;
    System.out.println(wow2.z);
  }

}

class Wow2 extends Wow1 { }
 LocalWords:  args println

 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Peter van der Linden  
View profile  
 More options Apr 22 1997, 3:00 am
Newsgroups: comp.lang.java.programmer
From: lin...@positive.eng.sun.com (Peter van der Linden)
Date: 1997/04/22
Subject: Re: Is this a problem with me or java?

"private" means "this field is not accessible outside this class".

Your code that frobs the field is inside the class, so is presumed
to know what it is doing with one of its own fields.  If you moved
that code to the subclass, it would fail to compile in the way you
expect.

     Regards,

     Peter

Keith Allan Barrett  <keith...@wpi.wpi.edu> wrote:

--
Peter van der Linden      Java Programmers FAQ:   http://www.best.com/~pvdl
-disclaimer-
 unless stated otherwise, everything in above message is not personal opinion
 and is an official statement of Fun-Fun Novelty Robot Weapons Inc.

 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
gruban  
View profile  
 More options Apr 22 1997, 3:00 am
Newsgroups: comp.lang.java.programmer
From: gru...@bnl.gov ()
Date: 1997/04/22
Subject: Re: Is this a problem with me or java?

Keith Allan Barrett (keith...@wpi.wpi.edu) wrote:

: I would expect the following program to produce an error on
: the fact that objects shouldn't have access to their super
: classes private data. It seems in this case that a object
: can gain access to its private data if it is accessed from
: another object with the same class as its super class. I am

That's right. Private and Protected restrict access by Classes not Objects.
See more below.

: new to this and would appreciate any feedback I can get.

: class Wow1 {
:   private int z = 7;

:   public void test() {
:     Wow2 wow2 = new Wow2();

:     // Why can I access and modify the private properties of my super class
:     System.out.println(wow2.z);
:     wow2.z = 4;

You see, the "I" in your comment in this case, the accessing object,
is _not_ "wow2".  It is, in fact, the (new Wow1()) - if it had a name
it would be clearer.  "NewWow1" is a Wow1, and as such does have
all access privileges to all members to all Wow1's. This is true of
C++ as well.

Now say Wow2 had a private member q.  You could not access q in
Wow1.test() - but you could access it in any function of any Wow2, not
necessarily even the one which "owned" it.

Clearer now?

For a real challenge, try to figure out this one [posted in a different
thread, but really neat - credit would go to Jim Ortlieb
(o...@midway.uchicago.edu) except he just came up with the puzzle,
didn't figure out why it worked].

Since Java has such strong type casting rules,
why does the following compile?

public class bar extends Object {
        double x;
        public bar() {
                x = 0.0;
        }

}

public class foo extends Object {
        int y;
        foo() {
                y = 1;
        }

}

public class foobar extends Object {
        foo f;
        bar b;
        public foobar() {
                f = (foo)((Object)b);
        }

}

George Ruban
gru...@gte.com

(One hint about the puzzle - try debugging it.
 It's kind of a cheap trick :-( )


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Allan  
View profile  
 More options Apr 22 1997, 3:00 am
Newsgroups: comp.lang.java.programmer
From: Allan <ab...@nowhere.hursley.ibm.com>
Date: 1997/04/22
Subject: Re: Is this a problem with me or java?

I would say that you are not accessing the private properties of your
super class. test() is a method belonging to the class Wow1 and that is
why it can access z.

Allan.

--------------------------------
Allan Boyd
Java Technology Centre
IBM Hursley UK Laboratories


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
David Holmes  
View profile  
 More options Apr 23 1997, 3:00 am
Newsgroups: comp.lang.java.programmer
From: "David Holmes" <dhol...@mri.mq.edu.au>
Date: 1997/04/23
Subject: Re: Is this a problem with me or java?

gru...@bnl.gov wrote in article <5jj6se$...@news.gte.com>...

> For a real challenge, try to figure out this one [posted in a different
> thread, but really neat - credit would go to Jim Ortlieb
> (o...@midway.uchicago.edu) except he just came up with the puzzle,
> didn't figure out why it worked].

> Since Java has such strong type casting rules,
> why does the following compile?
> public class foobar extends Object {
>         foo f;
>         bar b;  // bar is not derived from foo
>         public foobar() {
>                 f = (foo)((Object)b);
>         }
> }

Because you told the compiler that it was okay to do the assignment by
putting in the explicit cast: first to Object and then to foo. As b is null
this actually safe at runtime. If b were not null then you'd get a
ClassCastException.

David


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Alexandre Oliva  
View profile  
 More options Apr 23 1997, 3:00 am
Newsgroups: comp.lang.java.programmer
From: Alexandre Oliva <ol...@dcc.unicamp.br>
Date: 1997/04/23
Subject: Re: Is this a problem with me or java?

Keith Allan Barrett writes:
> class Wow1 {
>   private int z = 7;
>   public void test() {
>     Wow2 wow2 = new Wow2();
>     // Why can I access and modify the private properties of my super class
>     System.out.println(wow2.z);
>     wow2.z = 4;
>     System.out.println(wow2.z);
>   }
> }
> class Wow2 extends Wow1 { }
>  LocalWords:  args println

Because access control in Java is done in a class basis.  Since method
test is in class Wow1, it has access to any method or field in class
Wow1, even for instances of subclasses of Wow1.

--
Alexandre Oliva
mailto:ol...@dcc.unicamp.br mailto:aol...@acm.org
Universidade Estadual de Campinas, SP, Brasil


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »