aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Rules for Protected and Default Members Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Rules for Protected and Default Members" Watch "Rules for Protected and Default Members" New topic
Author

Rules for Protected and Default Members

Brian Reindel
Greenhorn

Joined: Jan 04, 2007
Posts: 6
I am jotting down notes while studying for the SCJP exam, and I want to be sure that I get this concept 100%. Would it be correct to make the following two statements:

"A default member of a public class can only be accessed by a class or a subclass within the same package."

"A protected member of a class can be inherited by a subclass in a different package, but it cannot be referenced."

If this is not completely correct, or you have alternate ways of stating these two rules, please let me know.

Thanks
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Welcome to JavaRanch!

I'm moving this to our SCJP forum for special attention. (See links at top of page to continue discussion there.)


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
Qunfeng Wang
Ranch Hand

Joined: Jan 28, 2005
Posts: 433
I think you are right.

There is a good explanation in Ch1 of K&B's book.


To be or not to be. It's a question.
Priya Viswam
Ranch Hand

Joined: Dec 28, 2006
Posts: 81
>>"A protected member of a class can be inherited by a subclass in a different package, but it cannot be referenced."

Protected member can be referenced in the subclass even if the subclass is in a different package.


SCJP 1.5<br />SCWCD 1.4
Brian Reindel
Greenhorn

Joined: Jan 04, 2007
Posts: 6
Originally posted by Priya Viswam:
>>"A protected member of a class can be inherited by a subclass in a different package, but it cannot be referenced."

Protected member can be referenced in the subclass even if the subclass is in a different package.


Priya, if what you say is correct, then why in the book, does this cause an error:


[ January 04, 2007: Message edited by: Brian Reindel ]
Priya Viswam
Ranch Hand

Joined: Dec 28, 2006
Posts: 81
The example shown by you is correct.

From the statement "A protected member of a class can be inherited by a
subclass in a different package, but it cannot be referenced.""
what i understood is that you can't access the protected variable in the subclass.

If you are using a reference of the parent class then it is not inheritance.
That is the reason why you can't access that in your child class.
Harish Paravasthu
Greenhorn

Joined: Dec 21, 2006
Posts: 23
Yes priya is right,Protected member in one package can be accssed in other package only by inheritance and not by reference of suerclass...

harish


Harish Paravasthu
Brian Reindel
Greenhorn

Joined: Jan 04, 2007
Posts: 6
Originally posted by Harish Paravasthu:
Yes priya is right,Protected member in one package can be accssed in other package only by inheritance and not by reference of suerclass...

harish


Which would mean that my original two statements are correct.

Thank you for everyone's response and for the assistance.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Rules for Protected and Default Members