aspose file tools*
The moose likes Beginning Java and the fly likes over loading Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "over loading" Watch "over loading" New topic
Author

over loading

surya goutham datta rejeti
Greenhorn

Joined: Oct 30, 2008
Posts: 4
public class Test {

public void takeMethod(String s) {
System.out.println("A");
}

public void takeMethod(Object s) {
System.out.println(s);
}
public static void main(String[] args){
Test test= new Test();
test.takeMethod(null);
}
}
This is working fine with "A" as out put. Why so, both String and Object can take null.

Then why not
public class asd {


public void takeMethod(Test s) {
System.out.println("A");
}

public void takeMethod(NewOne s) {
System.out.println(s);
}
public static void main(String[] args){
asd test= new asd();
asd.takeMethod(null);
}

}
class Test {
}
class NewOne{}

It is compile error .compiler ambiguity
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19670
    
  18

The compiler always tries the most specific version. In the first example that's String, and it works because String extends Object.
In the second example, there is no relation between Test and NewOne, so the compiler is at a cross roads and doesn't know which way to go.

Test it, let Test extend NewOne. You'll see it will work.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: over loading