File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes String vs Object 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 "String vs Object" Watch "String vs Object" New topic

String vs Object

mvPrasad Regula
Ranch Hand

Joined: Sep 14, 2009
Posts: 42
Why is the code below printing String version, as both String and Object accept null value.

Prasad Regula
Bear Bibeault
Author and ninkuma

Joined: Jan 10, 2002
Posts: 63846

"myPrasad Regula", please check your private messages for an important administrative matter or your account will be removed.

[Asking smart questions] [About Bear] [Books by Bear]
Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

If I able to reply....
Check the code below......

The method, which has the lower place in the hierarchy will be invoked..... In this example, Class C is the lowest class in the hierarchy. So the method with that type of parameter will be invoked. Same for your coding, as String is a sub class of Object.......

|BSc in Electronic Eng| |SCJP 6.0 91%| |SCWCD 5 92%|
Harpreet Singh janda
Ranch Hand

Joined: Jan 14, 2010
Posts: 317

Yes, Abimaran Kugathasan is right. Always more specific version of method is called and because whenever you are coming down in a hierarchy you a more specific about the class. So always the method with argument of lowest level of the hierarchy is called.
Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

Muhammad Khojaye
Ranch Hand

Joined: Apr 12, 2009
Posts: 449

See also this FAQ
I agree. Here's the link:
subject: String vs Object
It's not a secret anymore!