GeeCON Prague 2014*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes protected can not be accessed in derived ?? 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 "protected can not be accessed in derived ??" Watch "protected can not be accessed in derived ??" New topic
Author

protected can not be accessed in derived ??

JigaR Parekh
Ranch Hand

Joined: May 23, 2005
Posts: 112
why protected can not be accessed in derived class.



Select 1 correct option.

a It will print 10.
b It will print 20.
c It will not compile.
d It will throw an exception at Run time.
e None of the above

I have selected answer b but correct answer is c why ???
Steve Morrow
Ranch Hand

Joined: May 22, 2003
Posts: 657

Protected access can be tricky. You can only access protected members from within the context of the inherited class. For instance, you can access B's 'i' variable (inherited from A), but not A's through an A reference, since B is in a separate package.

From the JLS:

�6.6.2 Details on protected Access
A protected member or constructor of an object may be accessed from outside the package in which it is declared only by code that is responsible for the implementation of that object.

Hope this helps!
JigaR Parekh
Ranch Hand

Joined: May 23, 2005
Posts: 112
thx for explanation
Smitha Ballikar
Ranch Hand

Joined: Aug 02, 2005
Posts: 99
hi,

I didnt understand the explanation. Can somebody explain why answer is not 20.

Cheers
Smitha
Steve Morrow
Ranch Hand

Joined: May 22, 2003
Posts: 657

The answer is not 20, as the B class is not allowed access to A's i variable. It's outside the package, and the code that attempts to access the i variable is not responsible for the implementation of the A type.
Ryan Kade
Ranch Hand

Joined: Aug 16, 2005
Posts: 69
Here's my take on it as well:

http://www.coderanch.com/t/250316/java-programmer-SCJP/certification/Confused-protected-modifier
Smitha Ballikar
Ranch Hand

Joined: Aug 02, 2005
Posts: 99
Thanks!! Got it now..
srikanth reddy
Ranch Hand

Joined: Jul 28, 2005
Posts: 252
thanks parekh ...

nice question


thanks & regards

srikanth reddy


Thanks & Regards<br /> <br />-Srikanth
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: protected can not be accessed in derived ??