• 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:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

method invocation

 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
class GFC211 {} class GFC212 extends GFC211 {}
class GFC213 extends GFC212 {
static void m(GFC211 x, GFC211 y) {System.out.print("GFC211,GFC211");}
static void m(GFC211 x, GFC212 y) {System.out.print("GFC211,GFC212");}
static void m(GFC212 x, GFC211 y) {System.out.print("GFC212,GFC211");}
static void m(GFC212 x, GFC212 y) {System.out.print("GFC212,GFC212");}
static void m(GFC211 x, GFC213 y) {System.out.print("GFC211,GFC213");}
public static void main(String[] args) {
GFC213 gfc213 = new GFC213(); m(gfc213, gfc213);
}}


i think method m(GFC212 x, GFC212 y) will be invoked.
but the output is compile time error??
why is it an error???
 
Ranch Hand
Posts: 128
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by shameena Hussain:
i think method m(GFC212 x, GFC212 y) will be invoked.
but the output is compile time error??
why is it an error???



Hi Shameena,
Well the reason for compile time error is that the method m() has been overloaded in the class GFC213.

All overloaded method calls are resolved at compile time only. When the compiler sees a number of versions of the method m() having the same signature, it cannot distinguish between them and hence cannot decide which method should be appropriate to call.

So it gives a compile time error saying that reference to method m() is ambiguous.

BTW, why do u think that a method would be called ? Overloading is always taken care of at compile time.

Hope this helps.
 
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sherry's right, of course, there are too many m() methods that have signatures matching your parameters, but I want to explain a little more about why they are considered to be ambiguous. (I'm a little new at this, so someone please correct me if I'm wrong.)

Let's use a modified example of your code, with simpler method identifiers:



Trying to compile this code will give you the error "The method m(A, A) is ambiguous for the type C".

When the compiler is stepping through its method resolution process, it determines which methods may apply based on the signature. In this case, that means all 5 m() methods. Next, if it finds that there is more than one applicable signature, it tries to determine which method is most specific based on the following general rule: "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."

In your case, you have two methods that are "equally" specific. We can toss out methods m(A,A) and m(A,B) because they both can be converted into (via method invocation conversion) by the other three. m(B,A) is also less specific because m(B,B) can be converted into it. I.e.,

m(B,A); m(B,B); m(A,C) can all be converted to m(A,A), thus making them more specific
m(B,B); m(A,C); can both be converted to m(A,B), thus making them more specific
m(B,B); can be coverted to m(A,B)

However, the specificity rules stop there. m(B,A) and m(A,C) can't be converted into each other. They are, in essence, "equally" specific, so the compiler can't decide which one to pick!

What can solve this is a new overloaded method:



m(B,C) can be converted up the chain to any of the previous 5 m() methods, including our two problem methods, m(B,A) and m(A,C), so it is more specific than all of them. Thus, that would be the one the compiler could pick.

For more on this, look at the JLS, specifically section 15.12.2.2, "Choose the Most Specific Method":

http://java.sun.com/docs/books/jls/second_edition/html/expressions.doc.html#20448

EDIT: Added link, corrected spelling
[ August 22, 2005: Message edited by: Ryan Kade ]
 
Ranch Hand
Posts: 961
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When I was studying for the exam found this tricky question, related to this subject. I guess you might find it useful for the Certification Exam.



Line 2 compile perfectly, but line 3 does not. Why? Because several method signatures could accept a null, that's to say: String(StringBuffer), String(StringBuilder), String(String), String(char[]), etc.

So, to which of all this does null correspond? Be careful with this tricky question.
 
Ryan Kade
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Null is a literal, not a datatype, so it doesn't correspond at all (like the old apples vs. oranges comparison). There's a difference between passing a null and passing a String object that has been initialized to point to a null.

It'd be akin to trying do the following:



It's not a perfect analogy, but it illustrates the point.
[ August 22, 2005: Message edited by: Ryan Kade ]
 
shameena Hussain
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thank you friends...
 
Ranch Hand
Posts: 252
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hello ryan...

i didnt get ur explanation for most specific method ...
u have talked about some "converted into it" ....i didnt get u...
i would be thankful if anyone can explain me that topic as i have read it
previously also but didnt get it ...

thanks & regards

srikanth reddy........
 
Ryan Kade
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry, srikanth, I'll try and be a little more clear.

When you make a method call, the compiler has to walk through a series of steps to find out which method you are actually referring to. During that process, if it can't find a method that exactly matches the method signature you called, then it starts to look for methods that your signature could be converted into.

Take my example. The call m(c, c) is looking for a method m that accepts two parameters, each of which will be of type class C. There is no such method m(C,C). However, since C is a subclass of B (which is in turn a subclass of A), other methods with A and B in their signature may qualify.

Now the compiler runs into another problem. It has found 5 methods it could potentially execute. All 5 defined methods can be "converted into" from m(C,C). That is, the two parameters c and c can each be converted into (and treated as though they were instances of) class A or B.

So how does the compiler choose which function to use? That's where the idea of specificity comes into play. The compiler decides, based on hierarchical rules, which method is the most specific, and then converts the method call to m(C,C) into that other method.

The problem with your example was, there were two methods with equal specificity, so the compiler couldn't proceed. The call to m(C,C) was ambiguous. The method I suggested adding resolved that ambiguity.

Does that make more sense? Feel free to ask more questions if it still isn't clear.
[ August 23, 2005: Message edited by: Ryan Kade ]
reply
    Bookmark Topic Watch Topic
  • New Topic