• 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

method overloading

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have a question,it is from barry boon's mock exam.I did not understand why the answer is 'c',here is the code
class Test {
public static void main(String[] args) {
Test t = new Test();
t.test(1.0, 2L, 3);
}
void test(double a, double b, short c) {
System.out.println("1");
}
void test(float a, byte b, byte c) {
System.out.println("2");
}
void test(double a, double b, double c) {
System.out.println("3");
}
void test(int a, long b, int c) {
System.out.println("4");
}
void test(long a, long b, long c) {
System.out.println("5");
}
}
The option given are
a)1
b)2
c)3
d)4
e)5
Anybody please explain me.
regards
vasanthi
 
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
First see what is being passed.
double,long,int
hope that makes it clear.
K Singh
 
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
its due to the automatic method call conversion
as the first argument is double so it cant be converted to anything..
second is long so it can ve converted to float and double so it finds fot the closest one
third is int . it can be converted to long, float and double
if there is some method as
test (double a, double b, long c){} -> this will be called instead of
test ( double,double,double)
HTH
 
Sheriff
Posts: 5782
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
vasanthi,
Your name does not comply with the JavaRanch naming policy. Please choose one that meets the requirements.
Javaranch appreciates your cooperation to comply with the official naming policy.
Ajith
 
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think here how it works...
The method calling is based on the first formal parameter value, i.e if test(1.1,2L,3) --> test (double,long,int) --> which would convert rest of the parameters to the first parameter's size and calls test(double,double,double). Now to prove this try
test(1.1f,2L,3) and have one more method test(float,float,float) and see the result...
BOL
Sivaram
 
Ranch Hand
Posts: 118
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
All of sustification given are correct
But i would like to add something...
while calling a method compiler will look for such set of
arguments so that it need not to cast the parameters..
and only method that is satisfying the ..
call to(double,long,int)
is..
(double,double,double)
hope it will be clear..

 
My name is Inigo Montoya, you killed my father, prepare to read a tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic