This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Overloading question... Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Overloading question..." Watch "Overloading question..." New topic
Author

Overloading question...

Dilshad Syed
Greenhorn

Joined: Mar 05, 2001
Posts: 29
Can someone explain the output of this program?
<CODE>
public class Test
{
public void method(Object o)
{
System.out.println("Object Verion");
}
public void method(String s)
{
System.out.println("String Version");
}
public static void main(String args[])
{
Test question = new Test();
question.method(null);
}
}
</CODE>
Sivalingam Sivasuthan
Ranch Hand

Joined: Jan 12, 2001
Posts: 75
Hi Dilshad Syed:
Both object reference and String reference can be assigned with null. JVM assumes that the null is used as a reference for a String Object.Because String extends the Object, So the Overloaded method with String argument will be called(at the runtime most Specific Version).
-Siva

- Siva<br /> Sun Certified Programmer for the Java™2 Platform
Dilshad Syed
Greenhorn

Joined: Mar 05, 2001
Posts: 29
So, is it safe to assume that:
In case of overloaded methods, if there is ambiguity at runtime, the JVM will ALWAYS call the overloaded method with the argument of the subclass
What would happen in the above example if there was no relationship between the arguments(String and Object) and both could still accept the same parameter(null)?
uma sakthi
Greenhorn

Joined: Feb 11, 2001
Posts: 10
Hai,
In that case the complier complains as reference is ambiguous(because both methods can handle and complier cannot decide)
Try using String and StringBuffer as the object types in your program.
Regards
uma
Narsimha Manekar
Ranch Hand

Joined: Mar 05, 2001
Posts: 35
Dilshad, I think you are right.
I have just modified your program to proove your statement.
// prog 1.
public class Test7
{
public void method(Test9 s)
{
System.out.println("Test9 Version");
}
public void method(Test8 I)
{
System.out.println("Test8 Version");
}
public static void main(String args[])
{
Test7 question = new Test7();
question.method(null);
}
}
class Test8{}
class Test9 extends Test8{}
Above program's output is: Test9 Version
//progr 2
public class Test7
{
public void method(Test10 s)
{
System.out.println("Test10 Version");
}
public void method(Test9 s)
{
System.out.println("Test9 Version");
}
public void method(Test8 I)
{
System.out.println("Test8 Version");
}
public static void main(String args[])
{
Test7 question = new Test7();
question.method(null);
}
}
class Test8{}
class Test9 extends Test8{}
class Test10 extends Test8{}
Above program gives compilation error, saying: Reference to method is ambiguous.
---Narsimha

Above
Originally posted by Dilshad Syed:
So, is it safe to assume that:
In case of overloaded methods, if there is ambiguity at runtime, the JVM will ALWAYS call the overloaded method with the argument of the subclass
What would happen in the above example if there was no relationship between the arguments(String and Object) and both could still accept the same parameter(null)?

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Overloading question...