• 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

weird overloading?

 
blacksmith
Posts: 979
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think the behaviour of the following overloading
example is not straightforward:

When TestAdder is launched the method addThem(short y, int x)
is called, while both methods can accept the values passed in
main(), and I don't get the compilation error "reference to
addThem is ambiguous..."
The compilation error does occur for example when the methods are
addThem(short, long) and addThem(int, int): "reference to addThem
is ambiguous, both method addThem(short, long) in Adder and method
addThem(int, int) in Adder match int result = a.addThem(b,c);"
Weird isn't it? Is there an explanation for this behaviour?
Gian Franco
 
Ranch Hand
Posts: 400
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There is some kind of implicit casting going on in the methods that compile.
Sorry but I can't remember exactly what the casting is.
 
Gian Franco
blacksmith
Posts: 979
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
can someone please explain the behaviour
of the posted code.
Imagine something like this in the SCJP
exam
Thanks,
Gian Franco
 
Ranch Hand
Posts: 330
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Gian,
A method invocation conversion can widen an argument to match the method parameters but since both overloaded methods can be applied the more specific method is chosen hence the method addThem(short y, int x) is called in lieu of addThem(int x, long y) because its method arguments is more specific to a.addThem(b,c).
If you change the method invocation to addThem(s, l) where s and l are short and long respectively, the compiler cannot decide which to choose because both methods can be applied and are specific to the method invocation.
I changed your code a bit to show you another example.

In the code above, although addThem(char y, short x) may seem specific to the method invocation a.addThem(b,c) (where b and c are short), the method that will be called is addThem(int x, long y) because method conversion invocation will not implicitly narrow the argument to match the parameter type of the method addThem(char y, short x).
 
Gian Franco
blacksmith
Posts: 979
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Dennis,
It's clear now.
Gian Franco
 
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Gian,
/* Will u plz tell me why first method is not more specific?
In first,compiler have to convert only second arg to long.
In second,both short values have to be promoted to int.
*/
class InvocationFunda{
static void xyz(short s1 , long l){}
static void xyz(int i , int i1){}
public static void main(String args[]){
short s1=10,s2=10;
xyz(s1,s2);
}
}
 
Ranch Hand
Posts: 270
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Harvinder Singh:
Hi Gian,
/* Will u plz tell me why first method is not more specific?
In first,compiler have to convert only second arg to long.
In second,both short values have to be promoted to int.
*/
class InvocationFunda{
static void xyz(short s1 , long l){}
static void xyz(int i , int i1){}
public static void main(String args[]){
short s1=10,s2=10;
xyz(s1,s2);
}
}


because for first parameter xyz(short s1 , long l) is more specific, but according to the second parameter, xyz(int i , int i1) is more specific and here comes the ambiguity....
 
Ranch Hand
Posts: 127
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ahoy!
Did anyone call for the ?


Choose the Most Specific Method
If more than one method declaration is both accessible and applicable to a method invocation, it is necessary to choose one to provide the descriptor for the run-time method dispatch. The Java programming language uses the rule that the most specific method is chosen.
The informal intuition is that 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.
The precise definition is as follows. Let m be a name and suppose that there are two declarations of methods named m, each having n parameters. Suppose that one declaration appears within a class or interface T and that the types of the parameters are T1, . . . , Tn; suppose moreover that the other declaration appears within a class or interface U and that the types of the parameters are U1, . . . , Un. Then the method m declared in T is more specific than the method m declared in U if and only if both of the following are true:
* T can be converted to U by method invocation conversion.
* Tj can be converted to Uj by method invocation conversion, for all j from 1 to n.
A method is said to be maximally specific for a method invocation if it is applicable and accessible and there is no other applicable and accessible method that is more specific.
If there is exactly one maximally specific method, then it is in fact the most specific method; it is necessarily more specific than any other method that is applicable and accessible. It is then subjected to some further compile-time checks as described in �15.12.3.
It is possible that no method is the most specific, because there are two or more maximally specific methods. In this case:
* If all the maximally specific methods have the same signature, then:
o If one of the maximally specific methods is not declared abstract, it is the most specific method.
o Otherwise, all the maximally specific methods are necessarily declared abstract. The most specific method is chosen arbitrarily among the maximally specific methods. However, the most specific method is considered to throw a checked exception if and only if that exception is declared in the throws clauses of each of the maximally specific methods.
* Otherwise, we say that the method invocation is ambiguous, and a compile-time error occurs.


[ January 16, 2004: Message edited by: Jim Crawford ]
 
Ranch Hand
Posts: 142
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Jim,
I don't understand this part of your explanation.
"However, the most specific method is considered to throw a checked exception if and only if that exception is declared in the throws clauses of each of the maximally specific methods. "
Can u explain this?
-Sanjana
 
Gian Franco
blacksmith
Posts: 979
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I was thinking about this 'mistery of the weird overload'
and I think I will stick to the following simple 'trick'.
First of all the premise that ambiguity or the choice of
a method is determined by the combination both by the
declaration of the methods as well as the parameters
passed in the method invocation.
Suppose the following:

In order to determine which addThem() method will be used:
1.) first think of the primitives ordered in ascending order
of bitdepth, for example: byte, short, int, long, ...
2.) then take one of the methods, say addThem(short y, int x)
and pretend its invoked with as addThem(byte,byte).
3.) For each parameter consider it's starting point it's
identical datatype in the bithdepth list in (1.) as position
zero. And add 1 until it reaches the datatype of the declaration.
byte to short is 1
byte to int is 2
4.) Sum the 'steps' for the chosen method (steps = 1+2 = 3)

5.) repeat the same for the remaining method, in our case
addThem(int x, long y).
byte to int is 2
byte to long is 3
steps = 2+3 = 5
6.) The method that needed less steps will be invoked
at runtime. In this case addThem(short y, int x).

If two methods have the same amount of steps then they
will be ambiguous, and the compiler will complain.
What do you think? (maybe it works with classes as
well, by ordering them in order of inheritance I still
have to try it though)
Gian Franco
 
Jim Crawford
Ranch Hand
Posts: 127
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by sanjana narayanan:
Hi Jim,
I don't understand this part of your explanation.
"However, the most specific method is considered to throw a checked exception if and only if that exception is declared in the throws clauses of each of the maximally specific methods. "
Can u explain this?
-Sanjana


I could pretend I know and go on bladibla about it, but to be honest, I only read the first part before I posted it and it sounded cool (i.e. like it explains the problem). That second part sounded a bit freaky or something. Don't think they will ask to that detail though. Just finished my exam. It went !
 
Police line, do not cross. Well, this tiny ad can go through:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic