Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Overloading question...

 
Dilshad Syed
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Dilshad Syed
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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)?

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic