File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Private member variable inherited?? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Private member variable inherited??" Watch "Private member variable inherited??" New topic

Private member variable inherited??

Michal Charemza
Ranch Hand

Joined: Jul 13, 2004
Posts: 86
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.

Vijay Vaddem
Ranch Hand

Joined: Feb 13, 2004
Posts: 243
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....


Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
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,

SCJP Tipline, etc.
I agree. Here's the link:
subject: Private member variable inherited??
It's not a secret anymore!