• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

extend from abstract class: son still has father's data members?

 
Castor Tack
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, all:



My question is: does class Son has the private int variable key?
I think Son should has his own int variable key, but I don't when I try to do manipulateKey(), eclipse gives error, saying that key is a private member of Father. Now I get confused: Son does or does not have his own key?

I tried to change the private modifier before key in Father to public, and I can do manipulateKey(). But does that mean I'm actually manipulating Father's key instead of Son's key?

And an extension to the problem: if key is a reference to a class, does that mean there's only one copy of date in memory and I change its value when I call manipulateKey() in both Son and Father? (Let's make father non-abstract now and add method manipulateKey() into it)

Thanks for your help.
 
Tom Reilly
Rancher
Posts: 618
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My question is: does class Son has the private int variable key?
I think Son should has his own int variable key, but I don't when I try to do manipulateKey(), eclipse gives error, saying that key is a private member of Father. Now I get confused: Son does or does not have his own key?
Son has the private int variable key (that it inherits from Father) but it cannot access it because it is declared private (so only methods of Father can access key)
I tried to change the private modifier before key in Father to public, and I can do manipulateKey(). But does that mean I'm actually manipulating Father's key instead of Son's key?
Yes. There is only one key. By declaring the key public, you are saying any class can access it.
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15216
36
Android IntelliJ IDE Java Scala Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
One of the most important concepts to remember with regard to inheritance in object oriented programming, is that it implies an "is a" relationship between the superclass and the subclass: an instance of a subclass is an instance of its superclass, or more specifically: a specialized version of an instance of its superclass.

For that reason, using names such as "Father" and "Son" for classes is confusing. If you write:

class Son extends Father

then what you're expressing is that a Son is a Father. If you think of the biological meaning of the term "inheritance", that doesn't sound right.

Inheritance in object oriented programming means specialization, which is not the same as inheritance in the biological meaning. Better naming would be, for example:

class Animal
class Dog extends Animal // a Dog is a certain kind of Animal

 
Castor Tack
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Many thanks to both of you.

I think I'm clear now.

And for the extended question, I did an experiment. Everything the son class inherits from father is within his own domain, i.e. when son is created, a deep copy (instead of shallow copy) of all data members in father is created.
 
Steve Luke
Bartender
Posts: 4181
21
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Castor Tack wrote:Many thanks to both of you.

I think I'm clear now.

And for the extended question, I did an experiment. Everything the son class inherits from father is within his own domain, i.e. when son is created, a deep copy (instead of shallow copy) of all data members in father is created.


In fact, there is no copy of data from the Father to the Son. When you do:

There is only one single Object (the son), not two distinct Objects (a Father and a Son). I think whatever test you used to determine if it is a deep or shallow copy was flawed. If you show the code you used, then maybe we can indicate where the misconception is.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic