Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

String vs Object

 
mvPrasad Regula
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why is the code below printing String version, as both String and Object accept null value.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64708
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"myPrasad Regula", please check your private messages for an important administrative matter or your account will be removed.
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
Clojure IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.......
 
Harpreet Singh janda
Ranch Hand
Posts: 317
Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2066
Clojure IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Muhammad Khojaye
Ranch Hand
Posts: 449
IntelliJ IDE Java Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
See also this FAQ
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic