Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Yaron Landesman
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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,

 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
Clojure IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
=> UseCodeTags and, polymorphism will not work with instance fields.
 
AmanZeeK Verma
Greenhorn
Posts: 29
Android Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
Yaron Landesman
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
Clojure IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Imad Aydarooos
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic