File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Value of instance variable of super used in subtype object referenced by super type Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Value of instance variable of super used in subtype object referenced by super type" Watch "Value of instance variable of super used in subtype object referenced by super type" New topic
Author

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

Yaron Landesman
Greenhorn

Joined: Nov 29, 2010
Posts: 4
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

Joined: Nov 04, 2009
Posts: 2066

=> UseCodeTags and, polymorphism will not work with instance fields.


|BSc in Electronic Eng| |SCJP 6.0 91%| |SCWCD 5 92%|
AmanZeeK Verma
Greenhorn

Joined: Dec 06, 2010
Posts: 29

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!


Aman ZeeK Verma
Yaron Landesman
Greenhorn

Joined: Nov 29, 2010
Posts: 4
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

Joined: Nov 04, 2009
Posts: 2066

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

Joined: Nov 02, 2010
Posts: 87
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Value of instance variable of super used in subtype object referenced by super type