• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

how to decide the method call?

 
Ranch Hand
Posts: 153
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi,
can u pl explain what would be the output of this code and why?
thanx.
ashok.
__________
public class bom {
public void method(Object o) {
System.out.println("Object Verion");
}
public void method(String s) {
System.out.println("String Version");
}
public static void main(String args[]) {
bom question = new bom();
question.method(null);
}
}
 
Ranch Hand
Posts: 151
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi ashok,
The output to ur code would be :"String Version"
Both String and Object can assigned null but since String class extends Object(Object being the superclass of all classes),
the method with String parameter will be executed since the nearest of the class in the hierarchy is called.
However if u have another overloaded form of this method with StringBuffer as its parameter then there will be compile error, because String and StringBuffer are peer classes but donot extend each other.
Hope this helps.
rajashree.
 
Ranch Hand
Posts: 356
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ashok,
I think this link might help you,
http://www.javaranch.com/ubb/Forum24/HTML/011170.html
Vanitha.
reply
    Bookmark Topic Watch Topic
  • New Topic