• 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

Overloading

 
Ranch Hand
Posts: 447
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Source : www.danchisholm.net


class GFC218 {
static void m(Object x) {System.out.print("Object");}
static void m(String x) {System.out.print("String");}
public static void main(String[] args) {m(null);}
}

The out put of this program is String.Here the method is overloaded,and the most specific one is String type so the output is String.




But the below program is giving compile time error.Can any body explain me how proceed with this type of question and also explain me the below question.


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);
}}

What is the result of attempting to compile and run the program?

a. Prints: GFC211,GFC211
b. Prints: GFC211,GFC212
c. Prints: GFC212,GFC211
d. Prints: GFC212,GFC212
e. Prints: GFC211,GFC213
f. Compile-time error
g. Run-time error
h. None of the above




Thanks

Anil Kumar
[ June 03, 2007: Message edited by: anil kumar ]
 
Ranch Hand
Posts: 3389
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Anil,

Yes. The answer is right. You can find the explanation for all these types of questions in the sample chapter given in the site. The same pdf is where this question also exists.

The reason with explanation as given in the pdf is below (fyi).


The method invocation expression, m(gfc213, gfc213), is ambiguous; because, no applicable method is more specific than all of the others. Method m(GFC212 x, GFC212 y) is more specific than m(GFC212 x, GFC211 y), because any invocation of m(GFC212 x, GFC212 y) could also be handled by m(GFC212 x, GFC211 y) without causing a compile-time type error. However, some invocations of m(GFC212 x, GFC212 y) can not be handled by m(GFC211 x, GFC213 y), so m(GFC212 x, GFC212 y) is not more specific than m(GFC211 x, GFC213 y). Furthermore, not all invocations of m(GFC211 x, GFC213 y) could be handled by m(GFC212 x, GFC212 y).




We need to check each and every statements for the most specific method which will suit the requirement.

Hope this helps.
 
anil kumar
Ranch Hand
Posts: 447
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi

Raghavan

I could not understand that explanation.If you are able to understand ,can you allorabate the answer.


Thanks

Anil Kumar
 
Raghavan Muthu
Ranch Hand
Posts: 3389
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Anil,

Consider the following program.



The about program produces the following output:


overloadedMethod(String)
overloadedMethod(String)
overloadedMethod(Object)
nonOverloadedMethod(String)



In line 1 to 2:
The method which is matched is the one which takes java.lang.String as it can deal with the arguments being supplied and it can pass it to the overloaded method which takes (java.lang.Object) without any compiler error.

In line 3:
Since there is an explicit object of type java.lang.Object passed, the method matched is one which takes java.lang.Object.

In line 4:
Nothing special. Just invoke a method which takes a String argument with a new String object.

If you compile the same program by uncommenting the last line of invocation in the main() method, the compiler gives an error saying that,


nonOverloadedMethod(java.lang.String) in checkOverLoadMostSpecificMethod cannot be applied to (java.lang.Object)



Because the actual argument defined in the method is java.lang.String but the formal argument what we are trying to pass is java.lang.Object. Since the superclass reference cannot be passed to subclass reference (as the subclass does not know how to deal with the superclass object), the compiler is unable to handle it by itself and thus an error.

If you are okay with the above code, you can understand the explanation given by the author.

The actual method invocation is m(gfc213, gfc213); - the one which is of type GFC213 for both. The method is called inside the class GFC213 which extends GFC212 which inturn extends GFC211. Now when the compiler tries to search for the applicable methods, lets see what happens.

Case 1:
  • (i) m(GFC211 x, GFC211y) - Yes its applicable because GFC211 is a supertype of GFC213. So this method can handle the invocation.


  • (ii) m(GFC211 x, GFC212 y) - Yes. Its applicable because both GFC211 and GCC212 are supertypes of GFC213.


  • (iii) m(GFC212 x, GFC211 y) - Yes. same as (ii)


  • (iv) m(GFC212 x, GFC212 y) - Yes. same as (ii).


  • (v) m(GFC211 x, GFC213 y) - Yes. its applicable because one of the arguments GFC211 is a supertype of GFC213 (being passed) and other is of same type.



  • When the compiler is now trying to find out the most specific method,
    till (iv), the method m(GFC212 x, GFC212 y) seems to be more speficic than that of (i) through (iii). Because if a call to (iv) (ie m(GFC212 x, GFC212 y) can very well be passed on to any of the three methods without any compile-time error as the arguments to the methods (i) through (iii) are either supertype or the same type of GFC212.

    But there is one method which is conflicting is (v) ie m(GFC211 x, GFC213 y). In this case, this method cannot handle the call from m(GFC212 x, GFC212 y) because one of the arguments (second) GFC213 is a subtype of GFC212. As per the first program, it cannot handle the invocation from m(GFC212 x,GFC212 y).

    Case 2:

    If you think why then the method (v) can not be chosen? m(GFC211 x, GFC213 y). Lets say, in this case if its chosen, then not all invocations can be handled by this because some of the other invocations (ii), (iii), (iv) are subtype of the first argument GFC211. As per previous scenario, it also does not hold good.

    Thats why the compiler is not able to find out the "most specific" method to handle the situation.

    Now if you read the reason what i posted in my first reply to this thread, you will understand.

    Also, you can just edit the same program by just commenting out some method definitions and by changing the reference types while invoking etc.

    Hope this helps.
    [ June 04, 2007: Message edited by: Raghavan Muthu ]
     
    anil kumar
    Ranch Hand
    Posts: 447
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Hi

    Raghavan
    This is the only point i could not understand.

    -------------------------------------------------------------------------
    In this case, this method cannot handle the call from m(GFC212 x, GFC212 y) because one of the arguments (second) GFC213 is a subtype of GFC212.
    -------------------------------------------------------------------------
    We are not calling any other method from this m(GFC212 x, GFC212 y) method.

    I could not understand this statement.


    Thanks

    Anil Kumar
     
    Raghavan Muthu
    Ranch Hand
    Posts: 3389
    Mac MySQL Database Tomcat Server
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Hi Anil,

    Eventhough there is no explicit call, its assumed because of overloaded methods there might be a call like 'super()' right. Thats why.

    Others please correct me if i m not on the track.
     
    anil kumar
    Ranch Hand
    Posts: 447
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Hi

    Raghavan

    super is a keyword which is used to call super class overridden method.I think that is not the case.Some other thing may be there.

    But i don't know


    Thanks

    Anil Kumar
     
    Raghavan Muthu
    Ranch Hand
    Posts: 3389
    Mac MySQL Database Tomcat Server
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Hey Anil,

    Thats right. I said 'like super()'. not exactly super. Thats why the most specific method is being searched for. And the explanation given for more specific method is "the call can be passed to the other overloaded method without any compile-time errors!". This is what i could infer and tell.
     
    Ranch Hand
    Posts: 1710
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Think the problem in Animal-Dog-Beagle way:



    There is no specific method for (Beagle, Beagle):

    Steps to solve this kind of questions:
    1- Quickly draw a UML, which class extends what class
    2- Search for the exact method for given parameter
    3- If not found, you work starts from here, search for the less specific
    method, but take care, when you make method selection, parameter passed to
    your method selection must suffice to call all other methods.


    we have:

    #1: Animal,Animal
    #2: Animal,Dog
    #3: Dog,Animal
    #4: Dog,Dog
    #5: Animal,Beagle


    We want Beagle,Beagle
    1- Exact match not found

    2- If we take #5, think is it ok to pick that; lets do check
    Any method call for Animal,Beagle will pass #1, #2 but sorry fail #3 and #4

    3- Move on, if we take #4, any method call with Dog,Dog will pass #1, #2,#3, #4 but sorry will fail #5 (Beagle is sub class of Dog)

    4- Move on, if we take #3, any method call with Dog,Animal will pass #1(as always), and #4 (of course) but fails for others.

    5- If we take #2, any method call with Animal,Dog will only pass #1 and #2
    and others fail.


    Thanks,
     
    Raghavan Muthu
    Ranch Hand
    Posts: 3389
    Mac MySQL Database Tomcat Server
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Hi chandra,

    thats a great explanation which explains the flow similar to that of the actual question.

    but i think i have also told the same steps and with which anil was unable to get one point. from your steps also, it is..


    parameter passed to your method selection must suffice to call all other methods.



    how about the call to other methods being done when we dont explicitly do it? the same he asked me too.

    Am i right Anil?
     
    anil kumar
    Ranch Hand
    Posts: 447
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Hi

    Chandra

    what is the meaning of this?
    -------------------------------------------------------------------------
    parameter passed to
    your method selection must suffice to call all other methods.
    -------------------------------------------------------------------------
    can you ellorabate this a little bit?



    Thanks

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

    Originally posted by anil kumar:
    Hi

    Chandra

    what is the meaning of this?
    -------------------------------------------------------------------------
    parameter passed to
    your method selection must suffice to call all other methods.
    -------------------------------------------------------------------------
    can you ellorabate this a little bit?
    Thanks

    Anil Kumar



    This step is the main concern the whole method selection is running
    around. Think in the way that there are so many overloaded methods
    available and you have to pick the most specific winning over all. The
    selected method promises that if it is selected, any method call that
    the selected method satisfies, can be used to all the remaining methods.

    Suppose there are two methods:
    #1 m(Animal, Animal)
    #2 m(Animal,Dog)


    And your method call is m(Dog, Dog) then obviously you will select #2 and
    it is selected on behalf of because it is more specific then #1. If you pass Animal, Dog #1 can also be used in absence of #1. Yeah the word absence is good to understand. Pick a method and think, I remove this (selected) method, then which can satisfy the method call, OK now if I remove that too, then the third one, ok if I remove that too fourth and so on. This is the way how more specific method selection is done easily.

    Thanks,
     
    Chandra Bhatt
    Ranch Hand
    Posts: 1710
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Raghvan said:


    Hi chandra,

    thats a great explanation which explains the flow similar to that of the actual question.

    but i think i have also told the same steps and with which anil was unable to get one point. from your steps also, it is..



    Raghvan, There is nothing special, I only poured my idea as I used to
    understand the things specially, I understood that question when I was
    doing Danchisholms questions couple of months ago.

    Your explanation matches to the original question that is good. I thought
    confusion might come because of lengthy class names while we can think
    in object oriented way (Beagle IS-A Dog IS-A(n) Animal).


    Thanks,
     
    anil kumar
    Ranch Hand
    Posts: 447
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Hi

    chandra
    ----------------------------------------------------------------------
    The selected method promises that if it is selected, any method call that
    the selected method satisfies, can be used to all the remaining methods.
    ---------------------------------------------------------------------------

    This is the point i am looking for, thanks

    see here in my example


    if i selected this
    static void m(GFC212 x, GFC212 y) {System.out.print("GFC212,GFC212");}

    and if apply that rule ,it fails.


    Am i write?

    Thanks
    Anil Kumar
    [ June 04, 2007: Message edited by: anil kumar ]
     
    Chandra Bhatt
    Ranch Hand
    Posts: 1710
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator


    if i selected this
    static void m(GFC212 x, GFC212 y) {System.out.print("GFC212,GFC212");}

    and if apply that rule ,it fails.
    Am i write?



    OK, if you selected that one, now apply the rule, does GF212,GF212 will
    satisfy m(GFC211 x, GFC213 y) that is one of the remaining methods.
    NO!!! so selection failed.

    Thanks,
     
    Raghavan Muthu
    Ranch Hand
    Posts: 3389
    Mac MySQL Database Tomcat Server
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Thanks Chandra.

    Anil,

    I think you have not read my reply (detailed) properly. If you read it again, you may get the explanation and reason why there is an error thrown by the compiler because of the inability to pick the most specific method!!

    But the reason for the most specific method to be able to satisfy or delegate the calls to all other applicable methods is a separate question which is what we asked later.

    You seem to mix both of them and get confused more. Aint I? Please read the replies (from me and chandra) again. You will get it.
     
    anil kumar
    Ranch Hand
    Posts: 447
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Hi

    Thanks Chandra and Raghavan.

    Ya Raghavan i did confuse by things extra and now i have idea how to deal with such type of questions


    Thanks

    Anil Kumar
     
    Greenhorn
    Posts: 12
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Raghavan,

    Superb explanation on this. Chandra added colour with different example. Few days back I had posted similar question. In this blog I found broader answer.

    I see diff posts with same question. Can JavaRanch come up with some filter while people adding same questions, so that they can search pervious blogs. It would be difficult but not impossible, but I think most of them refer same websites: javabeat, danchisholm. This way # of posts and many people time can be minimized.

    Just a suggestion.

    Me being new member, can spend hours in JavaRanch. Few are very good blogs.
     
    Greenhorn
    Posts: 14
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Hey the above was a cool way of sorting things out.
    Here is what i did

    class A{}
    class B extends A{}
    class C extends B{}

    public class overloaded
    {
    static public void ovMe(A a,A b){}
    static public void ovMe(A a,B b){}
    static public void ovMe(A a,C b){}
    static public void ovMe(B a,B b){}
    static public void ovMe(B a,C b){}
    static public void ovMe(C a,B b){}
    static public void ovMe(C a,A b){}
    public static void main(String...S)
    {
    ovMe(new C(),new C());
    }
    }

    In accordance with above explainations i always knew this was asking for trouble.But just couldnt understand the way compiler slapped:

    overloaded.java:16 reference to ovMe is ambigous,both method ovMe(A,C) in overloaded and ovMe(C,B) in overloaded match.

    Why did the compiler chose these two methods finally before throwing the error?
     
    Raghavan Muthu
    Ranch Hand
    Posts: 3389
    Mac MySQL Database Tomcat Server
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator


    Why did the compiler chose these two methods finally before throwing the error?



    Because, both of them are eligible for the method call with the objects of type 'C'. Thats why. Just uncomment the last two methods and compile, it will be alright as there is no confusion.

    HtH.
     
    reply
      Bookmark Topic Watch Topic
    • New Topic