This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Overloaded method call Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Overloaded method call" Watch "Overloaded method call" New topic
Author

Overloaded method call

Sandeep Desai
Greenhorn

Joined: Jun 14, 2005
Posts: 7
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


SCJP 1.4, SCWCD 1.4<br /><a href="http://www.thedesai.net/sandeep" target="_blank" rel="nofollow">http://www.thedesai.net/sandeep</a>
Ramakrishna Nalla
Ranch Hand

Joined: Apr 21, 2005
Posts: 61
Hi..i modified your code...now you can understand...pls go through the code:



OUTPUT:
null
object
Srinivasa Raghavan
Ranch Hand

Joined: Sep 28, 2004
Posts: 1228
Check this .This question is asked many times ..


Thanks & regards, Srini
MCP, SCJP-1.4, NCFM (Financial Markets), Oracle 9i - SQL ( 1Z0-007 ), ITIL Certified
Piyush Sam
Greenhorn

Joined: Jun 02, 2005
Posts: 24
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,


Piyush Jain<br /> <br />Being happy doesn't mean everything's perfect. It means you've decided to see beyond the imperfections.
parimelazhagan vinayagam
Greenhorn

Joined: Apr 19, 2004
Posts: 16
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 agree. Here's the link: http://aspose.com/file-tools
 
subject: Overloaded method call
 
Similar Threads
Why? null is not a common object,then what it is?
error- ambiguous reference to method
Overloading question...
why is String method called not Object method....?
Most specific method.......