| 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
Saloon Keeper
Joined: Aug 03, 2008
Posts: 9189
|
|
|
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!
|
 |
 |
|
|
subject: value of instance variable in super vs sub class
|
|
|