File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Method Overloading Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Method Overloading" Watch "Method Overloading" New topic
Author

Method Overloading

Savio Mascarenhas
Ranch Hand

Joined: Nov 29, 2000
Posts: 108
Q26.
public class Test
{
public void method(Object o)
{
System.out.println("Object version");
}
public void method(String s)
{
System.out.println("String version");
}
public static void main(String args[])
{
Test test=new Test();
test.method(null);
}
}
(a)Code does not compile.
(b)Code compiles cleanly & shows "Object version".
(c)Code compiles cleanly & shows "String version".
(d)Code throws a runtime exception.
The answer is (c).The value null can legally be assigned to both the overloaded methods then,how is it determined that the method taking a String argument should be invoked instead of the method that takes an Object argument ???
If the method taking an Object argument is replaced with any other object type(Integer,StringBuffer etc...)then,a compiler error "Reference to method is ambiguous" is displayed .


[This message has been edited by Savio Mascarenhas (edited December 08, 2000).]
Praveen Zala
Ranch Hand

Joined: Jul 02, 2000
Posts: 118
Savio,
Though I am unable to answer ur question, I will make it easier
for others to ponder on this..
I have the following code fragment in my program :
System.out.println("Strings");
String l[] = new String[1];
for(int i=0;i<1;i++)
System.out.println(l[i]);
System.out.println("Object");
Object ob[] = new Object[1];
for(int i=0;i<1;i++)
System.out.println(ob[i]);
The output of this is :
Strings
null
Object
null
This supports the fact that ARRAYS are AUTOMATICALLY initialized
in Java....by a default value......and since for strings and
object, it is "null" can anyone explain the constructor's
behaviour in Savio's code ?
Nice Qn Savio,
Praveen Zala
asim wagan
Ranch Hand

Joined: Nov 14, 2000
Posts: 62
<html><body>Please search through the archives it is present there.
It happens because java searches for the most specific class that can support a type of undefined object.Try the code elow and see it:
<code>
class a{
public void method(){
System.out.println("a version");
}
}
class b extends a{
public void method(){
System.out.println("b version");
}
}
public class c{
public static void main(String arg[]){
a a1=new a();
a a2=new b();
a1.method();
a2.method();
}
}
</code>
</body></html>
Adrian Yan
Ranch Hand

Joined: Oct 02, 2000
Posts: 688
The way JVM does is to use most specific methods.
public class Testing1 {
public void addOne(int i) {
i++;
System.out.println("int version");
}
public void addOne(long l) {
l++;
System.out.println("long version");
}

public static void main(String args[]) {
Testing1 t = new Testing1();
t.addOne(1);
}
}
The output is int version.
Even the call t.addOne(1), the argument 1 can be converted to long.
Aparna Narayanan
Ranch Hand

Joined: Nov 07, 2000
Posts: 44
Hi Savio,
Actually, as Adrian said, it searches for the most specific match. But, I guess, some more explanation is required. It checks whether the actual argument passed can be substituted for the lower most class in the hierarchy of formal parameter classes. If yes, then it substitutes.
In your code, String is the lower most in the hierarchy of classes(Object and String) used in the overloaded methods. So, it substitutes for String and you get the output as "String Version"
whereas, when you use Integer, Byte, Short, String which are all in the same level of hierarchy, there's no question of which is the "single" lowermost in the hierarchy and hence it reports of an ambiguity in using one of the overloaded methods.
Hope I am clear.
Aparna
 
 
subject: Method Overloading
 
Similar Threads
Mock test Q
Doubt in overloaded method related question
Constructor Overloading
question in javaquiz
String and Object class