wood burning stoves*
The moose likes Beginning Java and the fly likes Inheritance, private fields Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Inheritance, private fields" Watch "Inheritance, private fields" New topic
Author

Inheritance, private fields

You Gin
Ranch Hand

Joined: Jul 23, 2010
Posts: 52
Hi guys!

Could you please help me with my concern about inheritance of private members. It is stated that: "A subclass does not inherit the private members of its parent class. However, if the superclass has public or protected methods for accessing its private fields, these can also be used by the subclass."

I don't understand why the private variable isn't inherited while you are able to access it using an inherited public method?

For example, we have two classes:



Now let's create two objects:



For both of these objects I can use:



So I can get private variable with bObj. So the variable is inherited, otherwise how did I get an access to it?

I realise that child object contains an object of parent class in it... so I'm just confused about the statement "a subclass doesn't inherit the private members of its parent class". Can you clarify this for me please?

Thank you!

Mina Daoud
Ranch Hand

Joined: Sep 24, 2010
Posts: 88
They added this constraints to give the developer more options, if you are the author of class A and you don't want the subclass to access the local variables so you make it private, but if you want to pass these variable to specific classes you use the getter methods.
I hope i make sense.
You Gin
Ranch Hand

Joined: Jul 23, 2010
Posts: 52
Yes, this is clear, thank you. But what I meant is that why it is stated those private instance variables aren't inherited while you still can have an access to them using inherited public method. Let me ask it another way - where does this not inherited private instance variable "live"? Definitely it sholud "live" in an object of child class? If so - the private variable is inherited.
Mina Daoud
Ranch Hand

Joined: Sep 24, 2010
Posts: 88
These methods just let you read the variables which are private in the superclass. hence, these variables are not part of the subclass, as you can't re-assign values to them for example!
rahul sdfsdf
Greenhorn

Joined: Sep 27, 2010
Posts: 3
hi ,

private member not inherited that means you are not able to access those variable directly by the object of child class.
You Gin
Ranch Hand

Joined: Jul 23, 2010
Posts: 52
But still they are living inside of child object, right?
Mina Daoud
Ranch Hand

Joined: Sep 24, 2010
Posts: 88
I just debugged your code, within the LIVE objects is the object.getA() and it has value of "a". That means the live object is the method!
rahul sdfsdf
Greenhorn

Joined: Sep 27, 2010
Posts: 3
you can say that 'they are living inside of child object' because child object has the only reference of public or protected variable or function of base class in that function you can have the reference of private member of base class.
Shanky Sohar
Ranch Hand

Joined: Mar 17, 2010
Posts: 1051

Hey,Rahul.Welcome to JavaRanch


SCJP6.0,My blog Ranchers from Delhi
You Gin
Ranch Hand

Joined: Jul 23, 2010
Posts: 52
Well, it seems like clear, thank you guys.
 
jQuery in Action, 2nd edition
 
subject: Inheritance, private fields
 
Similar Threads
Are static members inherited?
Private variables aren't inherited?
confusion of protected
Super class for all the classes in java
abstract and static