• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

FIeld variable inherited in java..

 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
Clojure IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator



The result is : How are you
Good Bye


Could anybody explain in details? Thanks in Advanced!
 
Raju Champaklal
Ranch Hand
Posts: 521
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
what didnt you understand? field variables are never overridden....and which variable to invoke depends on the reference variable....
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
Clojure IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yaa, Field variables are not overriding, there are hiding. OK, Here I have problem with, how the method toString() returns Good Bye rather than How are you ? This thing, I couldn't understand! Please Explain! Thanks in Advanced!
 
Ankit Garg
Sheriff
Posts: 9528
32
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

See the arrows if you understand it...
 
Raju Champaklal
Ranch Hand
Posts: 521
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
since you are redefining the string greeting in your subclass the method toString will invoke the one redefined string.....

i just know that it will be Goodbye...i dont know the exact explanation...
 
Paul Prusko
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The toString() method called in

is invoked on obj reference which is of type AB but refers to actual object instance of type BA which is a subtype of the former. As the subtype has got the toString method overriden in the BA subclass this is what gets called at runtime. That's why you can see the output Good Bye which greeting instance variable of BA is initialized to and is not changed ever after.

Regards,
Paul.
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
Clojure IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot.....
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic