• 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
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

questions about overloading

 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In a class, there are a lot of methods with the same name and their parameters's number is also the same.
what's the algorithm that java choose method?

when there are implicit change,explicit change,boxing.
what's the algorithm that java choose method?
 
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi buddy

try to be precise.
When there are several methods are by same name even no of parameters are same but atleast one parameter must be different in order to use method overloading facility. return type, throwing exception type and access specifier may changed.
Suppose

-----------------------------------------------------
class
{
void method(int i1,int i2,int i3){}

void method(int i1,int i2,float f){}

void method(int i1,int i2,double l){}

public static void main(String args[])
{
method(1,1,1); //will invoke first method
method(1,1,1.0f) //will invoke second method
method(1,1,1,0) //will invoke third method
}
}
_________________________________________________________________

go through kathy sierra once for more clarification on this topic.
 
Storm Zcm
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
[Edit: non-English text elided. Please post only English.]

Refer to which methods to choose in overloading.
in my opinion.

First of all, the compiler consider exact the same.
the method's name must be the same,then the parameters's number must be the same,then the location and parameters type must be the same.
If all of these is the same, then choose this methods.

If there's implicit conversion.
It follow the
(byte,char,short)->int->long->float->double
to judge the parameters.

If there's reference types.
First find the reference type,the find whether is the subclass of the reference type.

In J2SE 5.0, there's auto-boxing. Then to judge the parameters.

[ July 17, 2008: Message edited by: Bear Bibeault ]
[ July 17, 2008: Message edited by: Bear Bibeault ]
 
Sheriff
Posts: 67752
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"As Angel", please check your private messages for an important administrative matter.

And please post only English text. Thanks.
 
Storm Zcm
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i'm sorry ,
i have changed my display name.
And I'll use only english in the forum.
 
Sheriff
Posts: 13411
Firefox Browser VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"Storm Zcm",
Please check your private messages regarding an important administrative matter.
-Ben
 
Ranch Hand
Posts: 201
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And note that its the method calls which make overloaded methods ambiguous as overloaded method calls are resolved at compile time, the compiler flashes error for any ambiguous call.
 
The only cure for that is hours of television radiation. And this tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic