• 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

method overloading

 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
friends,

I am trying to understand the following program. It's from Dan's sample chapter on Method Overloading.

class GFC200 {}
class GFC201 {
static void m(Object x) {System.out.print("Object");}
static void m(String x) {System.out.print("String");}
static void m(GFC200 x) {System.out.print("GFC200");}
public static void main(String[] args) {m(null);}
}

a. Prints: Object
b. Prints: String
c. Prints: GFC200
d. Compile-time error
e. Run-time error
f. None of the above

Answer : d. compile-time error

The description, as given by Dan, says that the argument 'null' could be converted to any of the three methods having arguments, 'Object', 'String' or 'GFC200'. But as none of them are more specific, it will end in compile-time error.

I also saw another sample question where it speaks about two methods having arguments as 'Object' and 'String'. In this case, if we pass 'null', the method having argument as 'String' will receive 'null' argument and it will compile without error.

If that's the case what's wrong with the above question and why can't the method that contains 'String' argument accept the 'null'? This is little confusing me.

Would appreciate, if anyone could help me explain this.

Thanks in advance

Joe
 
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Joe,

Check this out.
 
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That link doesn't really help me.. I read it and understand it but it doesn't seem to answer the original authors question. Interestingly,

class GFC200 {}
class GFC201 {

//static void m(Object x) {System.out.print("Object");}
static void m(String x) {System.out.print("String");}
static void m(GFC200 x) {System.out.print("GFC200");}
public static void main(String[] args) {m(null);}

}

Does NOT compile- it is the conflict between String and CGC200 which causes the problem, the Object is irrelevant. This does compile:

class GFC200 {}
class GFC201 {

static void m(Object x) {System.out.print("Object");}
//static void m(String x) {System.out.print("String");}
static void m(GFC200 x) {System.out.print("GFC200");}
public static void main(String[] args) {m(null);}

}

It must have something to do with how it interprets "Object" and somehow NULL is closer to both GFC200 and String than it is to Object, but why I can not say.

[ September 02, 2004: Message edited by: Tom Tolman ]
[ September 02, 2004: Message edited by: Tom Tolman ]
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Tom Tolman:
...somehow NULL is closer to both GFC200 and String than it is to Object, but why I can not say.



Are you forgetting that String extends Object?
 
Tom Tolman
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I get that both String and the other class extend Object, which makes them different than object. What I fail to understand is why an interface which is expecting an object vs a string would have a different "resolution of ambiguous call" on null. Wouldn't null match both an object and a string?
 
francis joseph
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You are right, null matches both an object and string. I am trying to explain from what I have gathered and not sure if this is the real reason.

Since, String is more specific than Object, it's the method that takes argument as String will be invoked. In my posting above, since there are 2 objects (String & GFC200) and both accepts null the compiler gets confused and throws ambiguous method call error. It's the case with any object other than the real Object. Even if we replace String object with any other object, the same error will be resulted.

In conclusion, for any two objects other than java.lang.Object, it will throw compiler error.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic