Re: Study Question Help
Steve wrote:
I have a question about a quiz question I took in a book. The book
states that the out statement will NOT cause a compiler error. I'm
thinking it does, because Fez inherits b as a private member and you
can't output a private with the dot operator. Where am I wrong?
It's not 'private'.
Does the compiler allow it since the out statement is in Fez, so it
is really not "exposing" something private to another class?
Nearly, except for saying 'private'. See John's answer and the JLS reference
below.
[...]
package pA;
public class Foo{
protected int b = 6;
}
package pB;
import pA;
public class Fez extends Foo{
public static void main(String args[] ){
System.out.println("next == " + new Fez().b);
}
}
John B. Matthews wrote:
IIUC, Fez inherits b as a _protected_ member, hence it is visible:
<http://download.oracle.com/javase/tutorial/java/javaOO/accesscontrol.html>
You do understand correctly.
The class 'Fez' inherits protected members from its supertype(s).
<http://java.sun.com/docs/books/jls/third_edition/html/names.html#6.6.2>
"... code that is responsible for the implementation of that object ..." means
code "within the body of a subclass".
The 'System.out' is within the body of the subclass, being inside the member
method 'main()'. So it has access to inherited protected member 'b'. Of
course, being an instance member it requires the reference be via an instance,
hence the need for 'new'. (??6.6.2.1 requires that the object be of type 'Fez'
or a subtype of 'Fez', so 'new Foo().b' would fail there.)
--
Lew
Ceci n'est pas une fen??tre.
..___________.
|###] | [###|
|##/ | *\##|
|#/ * | \#|
|#----|----#|
|| | * ||
|o * | o|
|_____|_____|
|===========|