• 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

invocations prob::

 
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
see this code::
class An {}
class Bn extends An {}
class Cn extends Bn {
static void m(An x, An y) {System.out.print("AA");}
static void m(An x, Bn y) {System.out.print("AB");}
static void m(Bn x, An y) {System.out.print("BA");}
//static void m(Bn x, Bn y) {System.out.print("BB");}
public static void main(String[] args) {
An a;
Bn b;
m(null,null);
m(a=null,b=null);
m(b, a);
}
}
here in this code when i comment out static void m(Bw x,Bw y)
i get compile time err which says
Cn.java:11: reference to m is ambiguous, both method m(An,Bn) in Cn and method m
(Bn,An) in Cn match
but if i remove the comment code executes well and calls the commented out method where m(null,null) is specified
my question here is how come the compiler know which method to call if null is specified in the method call and is it legal??
plz give simple explaination.
 
Ranch Hand
Posts: 326
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

The call m(null, null) is ambiguous in the above code because the compiler can't decide which method to invoke. This is because Bn extends An and, as such, is more specific than An. null may be assigned to any object reference and there are 2 methods that accept an An and a Bn, which are more specific than m(An, An), so the compiler gets confused and throws an error.

Here I've changed An to extend Bn and Bn to be the base class, the code will compile because An is now more specific than Bn, and the compiler can find one and only one method that is most specific: m(An, An)
[ September 17, 2003: Message edited by: Ray Stojonic ]
 
Ranch Hand
Posts: 787
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The basic principle is that compiler will try to find a method that matches exactly the signature of calling command. E.g.
Cn c = new Cn()
m(c,c)
a method with following signature will be sought for:
m(Cn c, Cn c) { } // method 1
However, if that is not available, following can be invoked:
m(Bn b, Bn b) { } // method 2
where both arguments c will be promoted to Bn type. If this is not available, following will do the job:
m(An a, An a) { } // methods 3
Now both argments will be promoted to An type.
Note that compiler will invoke a method that requires minimum number of promotions.
method 1 requires 0 promotions.
method 2 requires 2 promotions. One for each argument. c to b
method 3 requires 4 promotions. two for each argument. c to a
However if instead of methods 1,2 and 3 following are the only methods availble:
m(Bn b,Cn c) {} // method 4
m(Cn c,Bn b) {} // method 5
each one will require only one promotion. Hence compiler will get confused which one to pick. And you get compiler error.
In your example, nulls were passed. By definition null can be assigned to any type, therefore, they are valid arguments. However, there were two candidate methods that can be invoked with same number of promotions. And you go the compiler error.
Hope this makes sense.
Barkat
 
reply
    Bookmark Topic Watch Topic
  • New Topic