GeeCON Prague 2014*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Confused in Access control topic Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Confused in Access control topic" Watch "Confused in Access control topic" New topic
Author

Confused in Access control topic

Adil El mouden
Ranch Hand

Joined: Jun 01, 2005
Posts: 82
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 ]

SCWCD 1.4(Loading...), SCJP 1.4(98%), Bachelor of Engineering (computer science)
Priya dharshini
Ranch Hand

Joined: Aug 06, 2005
Posts: 78
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).


Thanks & Regards,
Priyadharshini . T
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
See the example in the Java Language Specification (JLS) 3.0 section 6.6.7 here.


Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
 
GeeCON Prague 2014
 
subject: Confused in Access control topic