• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

value of instance variable in super vs sub class

 
R Srini
Ranch Hand
Posts: 215
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 9521
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Access to instance variables is not polymorphic like non-static methods. It works just like static methods (this might help)....
 
saima kanwal
Ranch Hand
Posts: 148
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
R Srini
Ranch Hand
Posts: 215
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for the clarification, guys!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic