• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Rancher
Posts: 618
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 4179
22
IntelliJ IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
I carry this gun in case a vending machine doesn't give me my fritos. This gun and this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic