• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

instance variable inheritance

 
Andre Enimot
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, I got confused by a simple question, it is a modified question from K&B 6, Ch 2, Q13 self-test:

The output here is
furry bray
stripes bray
because polymorphism doesn't work for instance vars as we all know. But how does the code actually WORK? Does the actual Zebra object on the heap actually stores TWO values for the name variable??? and which one is returned depends on the reference type that was used??? If so, is this a general rule in cases when the inherited instance variable was "shadowed" in the subclass?
 
Neha Daga
Ranch Hand
Posts: 504
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Andre Enimot wrote: But how does the code actually WORK? Does the actual Zebra object on the heap actually stores TWO values for the name variable??? and which one is returned depends on the reference type that was used??? If so, is this a general rule in cases when the inherited instance variable was "shadowed" in the subclass?


No, the zebra object on heap does not store two values for name variable it just stores the value of its own varaible name.
which one is returned depends on reference type, as reference type here is of super class it will return the value from super class. Other than overridden methods nothing is returned or called from subclass in this type of polymorphic declarations.
 
Andre Enimot
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Let me rephrase the question.. This code creates ONE Zebra object on the heap, and this object has ONE variable name, whose value (whatever it is) is unchanged in lines 13-15. Then how is it even possible that in line 13 one gets "furry" and in line 15 one gets "stripes"??? Those two values must've been stored in the Zebra object (or somewhere else) and which one is retrieved depended on the ref variable type. Is my understanding correct?
 
Neha Daga
Ranch Hand
Posts: 504
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No you are not correct. Its true that you are not changing the value of variable name between line 13 - 15 but you are assigning the Zebra object to a Zebra reference variable at line 14 and hence now there is no polymorphism and line 15 will only see class Zebra's variable name which has value stripes.

Mammal's value is stored in mammal object and zebra's value is stored in zebra object. Now whichever reference is used the value will be retrieved from that very class.
 
Andre Enimot
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mammal's value is stored in mammal object and zebra's value is stored in zebra object

Hm but there is no Mammal's object since there is no new Mammal()

Now whichever reference is used the value will be retrieved from that very class

But these are instance (not static/class) variables, so their values normally can't be stored in classes..

So maybe when you say new Zeebra() the created object (besides it's own instance variable with their values) also carries the superclass variables with their values, and the value actually retrieved depends on reference type used???
 
Ankit Garg
Sheriff
Posts: 9521
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Neha Daga wrote:No, the zebra object on heap does not store two values for name variable it just stores the value of its own varaible name.

Actually the Zebra object in the heap will have two variables named name. One that is declared in Zebra class, and one declared in the Mammal class. When you access the value using a reference of type Zebra, the value in the Zebra class is retrieved. When you access this value using a reference of type Mammal, the value in the Mammal class is retrieved. If you call a method of Zebra class (using whatever type of reference variable), then the value of name in Zebra class will be used in that method. If you call a method of Mammal class, then the value of name in Mammal class will be used in that method...
 
Andre Enimot
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the clarification Ankit!
That's approximately what i thought... Is there a resource or book that discusses such details?
 
Neha Daga
Ranch Hand
Posts: 504
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks both of you to clear my misconception,
 
Ankit Garg
Sheriff
Posts: 9521
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Andre Enimot wrote:That's approximately what i thought... Is there a resource or book that discusses such details?

Till now I've read the K&B, Khalid Mughal and Core Java 2 books for these basics. Everything that I know is either from these books or what I learned from discussions on javaranch...
 
Robert Yabut
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I found a succinct answer to this question on the internet here: http://www.java-forums.org/new-java/21643-question-polyphormism.html by r035198x. He says quote:

"Basically instance variables are resolved using the compile time type while the run time type is used to resolve methods.
So since m is a Mammal at compile time, m.name resolves to "furry " while m.makeNoise() will call the Zebra version of the method."


I hope this helps because it sure did for me!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic