aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes instance variable inheritance Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "instance variable inheritance" Watch "instance variable inheritance" New topic
Author

instance variable inheritance

Andre Enimot
Ranch Hand

Joined: Jul 29, 2009
Posts: 31
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

Joined: Oct 30, 2009
Posts: 504
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

Joined: Jul 29, 2009
Posts: 31
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

Joined: Oct 30, 2009
Posts: 504
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

Joined: Jul 29, 2009
Posts: 31
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

Joined: Aug 03, 2008
Posts: 9313
    
  17

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

Joined: Jul 29, 2009
Posts: 31
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

Joined: Oct 30, 2009
Posts: 504
Thanks both of you to clear my misconception,
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9313
    
  17

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

Joined: Sep 20, 2010
Posts: 2
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!
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: instance variable inheritance