• 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:

Choosing More Specific Overloaded Methods Problem

 
Ranch Hand
Posts: 344
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


For the above coding why it is compile time error? I thought of GFC212,GFC212 as output, since it is more specific..

Can anyone explain why it is compile error?
 
Ranch Hand
Posts: 10198
3
Mac PPC Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It is obvious that the compiler will get confused when it sees such a code. The rule the compiler follows for most specific method is that,

Consider the following case,

myMethod(long kk, int ii)
myMethod(int aa, int pp)

Now considering the above code, the compiler knows that when you invoke myMethod(4L, 5), it knows that only one method that takes a long as argument works and without any ambiguity she calls the one that takes long as arg.

So, if you corelate the above small example with the one you have provided, when you invoke a method with a subclass object, there are many versions that the compiler can invole and if you invoke the same with a superclass object, there are again many versions that the compiler can try and invoke which is why it gets confused and splits out a compile time error.

Anyone to elaborate more on this?
 
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It would be much better if you could give better names to your classes.
 
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Notice that if you remove all but the last two methods, you still have a compile-time error.





According the JLS, the intuition is that one method is more specific than another if any invocation of it could be sent the other method without a compile-time error.

In this case, notice that there are parameters that you could send to one of these methods that could not be passed to the other.

Consider these pairs.



The first pair would be accepted by the second method (GFC211,GFC213), but it would not be accepted by (GFC212,GFC212) since a GFC211 is not a GFC212.

The second pair would be accepted by the first method (GFC212,GFC212), but it would not be accepted by (GFC211,GFC213) since a GFC212 is not a GFC213.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic