• 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

Function Overloading

 
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


I understand why this won't compile, but in the above example if i replace the "method(StringBuffer sb)" with "method(Object ob)", it compiles fine and selects method(Object ob) method over the method(String s)..how is this possible??
TIA
 
Ranch Hand
Posts: 3852
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Because compiler see whether I am able to recognise which method is going to get call or not , if not then produce error .

In first version ( String & StringBuffer ) , compiler was confuse because both are at same lavel .

But in second one , compiler detect that the method taking String is more specific than method taking Object for null ...

hope it make sense .
 
ankur rathi
Ranch Hand
Posts: 3852
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
BTW , In Java it is known as 'method overloading' not 'function overloading' .
 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I agreed with you when the parameters are (Object and Stirng). But if we changed from null to some string say "sample string" and using the parameters (StringBuffer and String) then also it should give compile time error as both String and StringBuffer are in the same level but it is working fine and calling method with String parameter. Can you explain this?

public class AQuestion
{
public void method(StringBuffer sb)
{
System.out.println("StringBuffer Verion");
}
public void method(String s)
{
System.out.println("String Version");
}
public static void main(String args[])
{
AQuestion question = new AQuestion();
question.method("sample string");
}
}
 
Sudhakar Krishnamurthy
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think it is behaving this way because by default "sample string" is a string and not a string buffer
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Sudhakar Krishnamurthy:
...if i replace the "method(StringBuffer sb)" with "method(Object ob)", it compiles fine and selects method(Object ob) method over the method(String s)...


Actually, calling method(null) invokes method(String) rather than method(Object). A null reference can be converted to any type of object, so the most specific method is used.

NOTE: Per Section 15.12.2.2 of the Java Language Specification, "one method declaration is more specific than another if any invocation handled by the first method could be passed on to the other one without a compile-time type error."

Ref: http://java.sun.com/docs/books/jls/second_edition/html/expressions.doc.html#18428
[ March 30, 2005: Message edited by: marc weber ]
 
marc weber
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Sudhakar Krishnamurthy:
I think it is behaving this way because by default "sample string" is a string and not a string buffer


Indeed. This is an exact match, so no type conversion is needed and there is no ambiguity.
[ March 30, 2005: Message edited by: marc weber ]
 
reply
    Bookmark Topic Watch Topic
  • New Topic