• Post Reply Bookmark Topic Watch Topic
  • New Topic

Private data members in a subclass  RSS feed

 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi:
I have a question about private fields in a super class.

I tested the code. It seems that the child class inherits the private field of Mother. However, the field can be modified only through the setter. What I want to understand is whether a child subclass inherits everything from a parent class, including private data members.

So

aChild should have a private field �name�. Am I correct? Thanks.
 
Ranch Hand
Posts: 1071
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, private fields are not inherited from the parents. In this case the super class has public getter and setter methods so your sub class can use these just as any other class could.

Create a private String name in your subClass, set it to some value, and then call getName and see what happens.
 
Yan Hu
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
However, a child object is also a parent object. A child has whatever the parent has. For example



Now, the child has a property "name"... Correct me if I am wrong.
 
Ranch Hand
Posts: 580
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you're talking about "property" in the sense that your class exposes a JavaBean-style property via getters/setters, then yes, you're correct. Your "property" is inherited by the subclasses. But, the storage of that property (a private member variable in this case) is not inherited. That's what Steven was saying. The superclass which defines the setters/getters can change how it stores the value however it wants (we call that encapsulation in the business).
 
Yan Hu
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you very much. I am working on a hibernate project. Inheritance and polymorphism mapping is very well supported in Hibernate. But I got confused a little bit. Now I got it straight..
 
Steven Bell
Ranch Hand
Posts: 1071
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Maybe this would help. Lets take your example:


Yes you can do:


The Child class does not store name, nor does it care how it is stored. Let's say that the Mother class is part of some third party library that you don't have source for and your Child class extends it.

In the new improved version of the Mother class name is no longer stored as a private String, but is now stored in a database. The child class is unaffected because it never dealt with the storage of the name to begin with.

Also notice that the Child class cannot access the name variable, it can only call the getters and setters. You can even have a name variable in the Child class and it will not effect the Mother class (unless you decide to override the getName and setName methods).
 
Yan Hu
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I got it now. Thank you very much.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!