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
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 <keit...@wpi.wpi.edu> wrote:
>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
--
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.
: 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
(or...@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 :-( )
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
gru...@bnl.gov wrote in article <5jj6se$c...@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
> (or...@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
> 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