• 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

Private member variable inherited??

 
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,

I am a bit confused about exactly what it means for a member variable to be inherited. If you have a class with an encapsulated private member variable with a public getter/setter, a subclass can still call the public getter/setter. However, the public getter/setter accesses the private variable of the superclass - so the subclass does have some sort of access to the private variable (and it does I have tested it).

However in my book (Sierra/Bates) and various threads, it says private members are never inherited.

Can someone explain (or point me to a good thread/website that explains) exactly what is happening when members are/are not inherited. I thought I understood it before I thought of the above senario.

Michal
 
Ranch Hand
Posts: 243
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Micheal,

Private members are never accessible directly from any other class.

However, you may access private members through its corresponding public
getter methods of the same class. (bcos the methods are public and are visible for any subclass).

Hope im making some sense....

Regards,

Vijay
 
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Private members are never inherited. Keep that in mind. That said, you can always do something like this:



Notice that, in that code, I'm able to access "a" from outside the class it is defined even though (1) the variable "a" is private and (2) the class that is performing the access is in no way related to the class where the private variable is defined.

The whole concept here is that, even though a is private, Encapsulation offers access to it via a public getter method. Notice, however, that there is no corresponding setter method. This means that, while another class may access the value of a, that class could never set the value of a. The concept of public getters/setters for private variables is nice because, in the end, you can control what variables are set to what within the class in which they are defined. If the variables are made public, you have no such control.

Now, back to your question. If a variable were to be truly inherited, you'd be able to access it via its simple name (assuming it isn't being hidden and even then you could still access it, just not with its simple name) without having to go through a public getter/setter method. Take a look at this:



In that snippet, you can see that the first println statement causes an error because the variable "a" is not inherited. Meanwhile, the variable "b" is inherited so it can be access via its simple name.

I hope this helps,
Corey
 
reply
    Bookmark Topic Watch Topic
  • New Topic