Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Rules for Protected and Default Members

 
Brian Reindel
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to JavaRanch!

I'm moving this to our SCJP forum for special attention. (See links at top of page to continue discussion there.)
 
Qunfeng Wang
Ranch Hand
Posts: 434
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think you are right.

There is a good explanation in Ch1 of K&B's book.
 
Priya Viswam
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
>>"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.
 
Brian Reindel
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 81
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Brian Reindel
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic