• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Overloaded method call

 
Sandeep Desai
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • 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
 
Ramakrishna Nalla
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi..i modified your code...now you can understand...pls go through the code:



OUTPUT:
null
object
 
Srinivasa Raghavan
Ranch Hand
Posts: 1228
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Check this .This question is asked many times ..
 
Piyush Sam
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • 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,
 
parimelazhagan vinayagam
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • 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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic