Forums Register Login

Value of instance variable of super used in subtype object referenced by super type

+Pie Number of slices to send: Send
Look at the following code. Why will it use the instance variable of the reference type, not the instance variable of the actual object? If that was a method, it would use the method of actual object. If you don’t try to “override” String s, you will get C as expected.
Thanks,

+Pie Number of slices to send: Send
=> UseCodeTags and, polymorphism will not work with instance fields.
+Pie Number of slices to send: Send
You cannot override variable. You can just 'hide them' (variable shadowing)

method is overridden and call is made based on object type. But variables are called based on reference (and visibility)

Hope this helps.
Please correct me if I am wrong!
+Pie Number of slices to send: Send
Hi,

This behaviour of instance variables is like of static variables. It looks as if there are two s Strings here, one for B and one for C, and the s of C is not the s inherited from B. But if Java uses late binding and the actual object instance is a C, how it gets the s of the B? I guess the answer requires some knowledge of JVM internals .

Thanks.
+Pie Number of slices to send: Send
 

Yaron Landesman wrote:Hi,

This behaviour of instance variables is like of static variables. It looks as if there are two s Strings here, one for B and one for C, and the s of C is not the s inherited from B. But if Java uses late binding and the actual object instance is a C, how it gets the s of the B? I guess the answer requires some knowledge of JVM internals .

Thanks.



Here, you have re-define the variable s, the variable will be inherited to the subclass, but, you can't expect the polymorphic invocation here, as like instance methods.
+Pie Number of slices to send: Send
 

Yaron Landesman wrote:Hi,

This behaviour of instance variables is like of static variables. It looks as if there are two s Strings here, one for B and one for C, and the s of C is not the s inherited from B. But if Java uses late binding and the actual object instance is a C, how it gets the s of the B? I guess the answer requires some knowledge of JVM internals .

Thanks.


as you know, for method overriding there are two methods one in the super and one in the sub with the same signature,so, actually there is first a cmpile time binding which bind the super type refrence to the method. At run time when the JVM checks the object it will finds out that its actually a sub object, but since the method signatures are the same the JVM is cabable of invoking the method on the sub object.
for the instance variable its a littile bit different. The first step will be done as usual: there is super refrence and the instance variable will binded to the super type refrence,but in this case at runtime what you will choose:
1- there is no Polymorphism ( means if there is two instance variable with the same name the runtime obect will see only the instance variables declaread in the class/interface of the refrence variable).
2- because of the above the binding will happen only at compile time with regard to the type of the refrence either its super type or sub type. and according to this either the super instance variable or the sub instance variable will show up.
3- if instead of accessing the instance variable like this(b.s), you used an instance method to print its value, then the story is different and the printed value will depend on the type of the object at run time: please try this code commented and un commented
Please do not shoot the fish in this barrel. But you can shoot at this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 1031 times.
Similar Threads
q from jiris.com on overriding
question about override
interface
Confusion.
use of super -Examlab Question
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 29, 2024 07:36:56.