• Post Reply Bookmark Topic Watch Topic
  • New Topic

Parent / Child relationship problem  RSS feed

 
Panseer Kaur
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
 
ahmed yehia
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.
 
Stan James
(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?
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!