• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Confused in Access control topic

 
Adil El mouden
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
This is a question from K&B book:

Given the following,



which statement is true?
A. The code compiles and runs, with output this 420 parent 420.
B. If line 8 is removed, the code will compile and run.
C. If line 10 is removed, the code will compile and run.
D. Both lines 8 and 10 must be removed for the code to compile.
E. An exception is thrown at runtime.

Answer given: C
I didn't understand the fact that p in line 10 cannot invoke its doStuff() method.

Thank you.

(formatted code)

[ August 07, 2005: Message edited by: Barry Gaunt ]
[ August 07, 2005: Message edited by: Adil El mouden ]
 
Priya dharshini
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The subclasses outside the package can't use a superclass reference to access a protected member. For a subclass outside the package, the protected member can be accessed only thr' inheritance.

The ParentUtil instance p cannot be used to access the doStuff() method.Because dostuff() has protected access, and the ChildUtil class is not in the same package as the ParentUtil class.doStuff()can be accessed only by instance of the ChildUtil class(a subclass of ParentUtil).
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
See the example in the Java Language Specification (JLS) 3.0 section 6.6.7 here.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic