Hi guys. Trouble understanding question #8 in Ch10 of the Kathy and Bert
SCJP book, included below. I thought the whole idea of
protected was to allow access, through inheritance (i.e., a child reference), outside of the package to member attributes and methods. If that's correct, how is that consistent with this example?
After typing this up, I think I see it ... the problem seems to be TestXcom is in a different package. protected means that if a child is in a different package than its parent, the child still has access to protected elements of its parent (though only through a reference of the child type). That privilege doesn't extend to a non-package object who happens to rightly hold a reference to the child. Thanks,
Mike
K&B Ch10#8 Given three files:
Which inserted at
will allow all three files to compile? (Choose all that apply.)
A.
B.
C.
D.
E. None of these options will allow the code to compile.
The correct answer is B justified as "The public access modifier is the only one that allows code from outside a package to access methods in a package -- regardless of inheritance."
[ July 20, 2008: Message edited by: Mike Mitchell ]