• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Protected / Private ?

 
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
CAN U EXAPLAIN BREIFLY
 
Tom McCann
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic