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 in super vs sub class 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 "value of instance variable in super vs sub class" Watch "value of instance variable in super vs sub class" New topic
Author

value of instance variable in super vs sub class

R Srini
Ranch Hand

Joined: Feb 19, 2010
Posts: 215
Hi all. I saw this question on Java Inquisition (http://enigma.vm.bytemark.co.uk/inquisition/index.php/Main_Page):



Question: What will the above code print? I was thinking that st.x would be 3 since x is a member variable. But its not. Can someone please help me understand why? TIA.
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9303
    
  17

Access to instance variables is not polymorphic like non-static methods. It works just like static methods (this might help)....


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
saima kanwal
Ranch Hand

Joined: Oct 05, 2009
Posts: 148
only instance methods are overridden , variables are never overridden. the st is superb type variable so it gets the value of x in superb class. it does not check what instance is in st variable.


SCJP 6.0 (86%) SCWCD 5 (94%)
R Srini
Ranch Hand

Joined: Feb 19, 2010
Posts: 215
Thank you for the clarification, guys!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: value of instance variable in super vs sub class