aspose file tools*
The moose likes Beginning Java and the fly likes extend from abstract class: son still has father's data members? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "extend from abstract class: son still has father Watch "extend from abstract class: son still has father New topic
Author

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

Castor Tack
Ranch Hand

Joined: Dec 06, 2010
Posts: 31
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.

The BEST user interface design I've ever seen? --- A flush toilet.
Tom Reilly
Rancher

Joined: Jun 01, 2010
Posts: 618
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

Joined: Aug 16, 2005
Posts: 14268
    
  21

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


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
Castor Tack
Ranch Hand

Joined: Dec 06, 2010
Posts: 31
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

Joined: Jan 28, 2003
Posts: 4181
    
  21

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.


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