Win a copy of TDD for a Shopping Website LiveProject this week in the Testing forum!
  • 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
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

Overloaded method call

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
public class Test {
public void a(Object o) { System.out.println("object"); }
public void a(String s) { System.out.println("string"); }

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

Why does the above code compile and produce the output of string?

Thanks,
Sandeep
 
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi..i modified your code...now you can understand...pls go through the code:



OUTPUT:
null
object
 
Ranch Hand
Posts: 1228
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Check this .This question is asked many times ..
 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi friends,

I modified the above program and introduce one more method which takes StringBugffer as an agrument and one method call which passes null, m getting compile time error that "The method a(Object) is ambiguous for the type Test"

I can understand that this is bcoz compile is not able to decide which method to call but if comment the method which takes StringBugffer as an agrument then it doest give any error, guys plz help me out to clear this

public class Test {
public void a(Object o) {
System.out.println("object");
}

public void a(String s) {
System.out.println(s);
}

public void a(StringBuffer buffer) {
System.out.println(buffer.toString());
}

public static void main(String args[]) {
String testStr = null;
new Test().a(testStr);
new Test().a(null); // compile time error
new Test().a(new Test());
}
}


Being happy doesn't mean everything's perfect. It means you've decided to see beyond the imperfections.

Regards,
 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Compiler searches for close matching for the given argument..Since null can be assigned to any Object,Compiler calls a(String s) ( String is subclass of Object).If Stringbuffer is used, String and StringBuffer both are subclasses of Object.[There is not child parent relationship between String and StringBuffer ] So compiler cant decide which method to call.So it throws error.


Not only a(StringBuffer) method...

For the following code also it will throw error

public class Test {
public void a(Object o) { System.out.println("object"); }
public void a(String s) { System.out.println("string"); }

public void a(Test a){ System.out.println("string123"); }


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

Because null now can be assigned to a(Test),a(String)...both are subclasses of Object...


And also see one more example

class Point { int x, y; }
class ColoredPoint extends Point { int color; }

class Test {
static void test(ColoredPoint p) {
System.out.println("(Inside ColoredPoint)");
}
static void test(Point p) {
System.out.println("(Inside Point)");
}

public static void main(String[] args) {

test(null);// compile-time error
}


}

It will call test(ColoredPoint)....I hope from these examples.u guys can understand..


Thanks
Pari
 
I have gone to look for myself. If I should return before I get back, keep me here with this tiny ad:
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic