Win a copy of Machine Learning Systems: Designs that scale this week in the Scala forum
or Xamarin in Action: Creating native cross-platform mobile apps in the Android forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Parent / Child relationship problem  RSS feed

 
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey everyone,

Just a question about how the parent child relationship works:

When someone creates an instance of a child does that mean specifically that all parents have an instance created? I ask because I have been doing the following and getting some strange results (at least to me they are strange)



The results it gives are:
5
2

I would expect that both should be 5 because I am still using my original object but just with using the parent class as a object reference type.

Can anyone shed some light on this?

Thanks
 
Ranch Hand
Posts: 424
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Accessing a variable depends solely on the refrence type Not the actual object type, because variables are shadowed not overriden.
 
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
See what this does. Instead of defining a new variable that shadows the original, it just sets the original in the default constructor:

Does it do what you expected?
 
Evacuate the building! Here, take this tiny ad with you:
Rocket Oven Kickstarter - from the trailboss
https://coderanch.com/t/695773/Rocket-Oven-Kickstarter-trailboss
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!