• 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

from mock exam qs..Jquest

 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
why does the following give the output..String version?
public class Test1 {
public void amethod(Object o){
System.out.println("Object version");}
public void amethod(String s){
System.out.println("String version");}
public static void main (String args[] ) {

new Test1().amethod(null);

}
}
Tejal
 
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That will print String Version. Because String is extends Object
class (ofcause not only Strings) Complier will select the most
Specific method that is the one which is taking String as an argument.
But if you put not related objects for example
void method (String s);
void method (StringBuffer sb);
and call the the method with null argument will results
Compilation error. as String and StringBuffer is not in the Hierchy.
Hope this helps.
Siva
 
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Having constructors taking String and StringBuffer arguments, will actually confuse the compiler, since both the arguments can be assigned to a null. So it will give a compiler error.
 
Tejal Shah
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks shiva and natchit
It has cleared my confusion
Tejal
 
reply
    Bookmark Topic Watch Topic
  • New Topic