• 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

Correct version of a method.

 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
public class AQuestion
{
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[])
{
AQuestion question = new AQuestion();
question.method(null);
}
}
Answers
1 The code does not compile.
2 The code compiles cleanly and shows "Object Version".
3 The code compiles cleanly and shows "String Version"
4 The code throws an Exception at Runtime.

The answer given is 3.
The default value for object reference is also null as it is for an uninitialized string reference.Why does, then, the compiler does not complain about ambiguous method call?
Thanks!

Teju
 
Ranch Hand
Posts: 149
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
From my understanding, both methods are applicable not actually ambigous as you have mentioned. The compiler will look for the most specific method that can accomodate the value passed in the parameter method.

Example on an ambigous call:


public class Ambigous{


void setNumber(long n) {
System.out.println(n);
}

void setNumber(int n) {
System.out.println(n);
}

public static void main(String args[]) {
new Ambigous().setNumber(2);
}
}

--Hope this help
 
arnel nicolas
Ranch Hand
Posts: 149
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry , i need to make a few correction with my example for ambiguous class above. Those codes will work fine. See my example below:

public class Ambiguous{

class AnotherObject{}

void aMethod(Object s) {
System.out.println("Object");
}

void aMethod(String s) {
System.out.println("String");
}


void aMethod(AnotherObject s) {
System.out.println("AnotherObject");
}

public static void main(String args[]) {
new Ambiguous.aMethod(null);
}
}

Hope this will help you.
 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
String is derived from Object and is therefore, more specific.
reply
    Bookmark Topic Watch Topic
  • New Topic