• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Overloading question...

 
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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>
 
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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)?
 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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)?


 
First, you drop a couch from the plane, THEN you surf it. Here, take this tiny ad with you:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic