wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes FIeld variable inherited in java.. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "FIeld variable inherited in java.." Watch "FIeld variable inherited in java.." New topic
Author

FIeld variable inherited in java..

Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066




The result is : How are you
Good Bye


Could anybody explain in details? Thanks in Advanced!


|BSc in Electronic Eng| |SCJP 6.0 91%| |SCWCD 5 92%|
Raju Champaklal
Ranch Hand

Joined: Dec 10, 2009
Posts: 521
what didnt you understand? field variables are never overridden....and which variable to invoke depends on the reference variable....


scjp 1.6 91%, preparing for scmad
"Time to get MAD now.. we will get even later"....by someone unknown
Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

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

Joined: Aug 03, 2008
Posts: 9293
    
  17


See the arrows if you understand it...


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Raju Champaklal
Ranch Hand

Joined: Dec 10, 2009
Posts: 521
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

Joined: Feb 16, 2008
Posts: 25
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

Joined: Nov 04, 2009
Posts: 2066

Thanks a lot.....
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: FIeld variable inherited in java..