aspose file tools*
The moose likes Beginning Java and the fly likes Protected / Private ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Protected / Private ?" Watch "Protected / Private ?" New topic
Author

Protected / Private ?

Tom McCann
Ranch Hand

Joined: Aug 27, 2005
Posts: 31
Hi,

I think I've got the hang of the 'protected' modifier but just want to make sure.

A subclass cannot inherit the private members of the superclass. Does this mean that all of the members that you wish to be 'inheritable' should be marked as protected ?

Obviously a standard technique is to mark variables as private, then provide public getter and setter methods to query and update these variables.

If that is the case, when I inherit, doesn't this mean I will get the getters and setters but not the variables themselves ? That wouldn't be much use. Is that why I need protected ?

I really want to nail this protected business.

Thanks for your advice.

Tom
Sant
Greenhorn

Joined: Jan 28, 2006
Posts: 15
CAN U EXAPLAIN BREIFLY
Tom McCann
Ranch Hand

Joined: Aug 27, 2005
Posts: 31
OK - here's my problem. I've just read through the explanation of the protected modifier in the SCJP Exam Guide (by Kathy Sierra and Bert Bates). I thought I had finally understood the protected modifier until I read the very last paragraph which says "it's only used in very special cases".

That's when my understanding came crashing down in a heap. I believe it is good practice to encapsulate access to instance variables. For example:



The variables are private and the get, set methods are used to access the values. OK, so far so good.

Now, what if I want to create a class in another package that extends Dog ?

For example:



To my understanding, Spaniel will inherit the getWeight and setWeight methods of the Dog class (because they are public) but WILL NOT inherit the weight variable because it is private.

So my question is: what good is it if I inherit the getter and setter methods but not the variable itself ?

Does this mean that I have to declare instance variables as protected if I want them to be inherited by classes in other packages ?

If that is the case, then why do the authors of the SCJP exam guide say that protected is used very rarely ?


I guess I am missing something very obvious here. I also cannot believe how much confusion the protected keyword causes. I've been learning java for many months and I still haven't read a satisfactory explanation.

Thanks in advance for your help with this toughie.
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
Originally posted by Tom McCann:

A subclass cannot inherit the private members of the superclass. Does this mean that all of the members that you wish to be 'inheritable' should be marked as protected ?


This is not quite true. A subclass inherits all members of the superclass. However, it cannot directly access private members of the superclass. In your example where the superclass provides setters and getters for the private members, the subclass can access them indirectly through these members. Notice that the private members still exist (i.e. there is memory allocated for them).

Layne


Java API Documentation
The Java Tutorial
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61437
    
  67

Santosh, please do not post in all uppercase. It makes your posts harder to read and some might misconstrue it as "shouting" (which could be considered rather rude).

thanks!


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Protected / Private ?