Results 1 to 12 of 12
  1. #1
    tripline is offline Member
    Join Date
    Oct 2011
    Posts
    19
    Rep Power
    0

    Default question about overload

    I know this is true
    int yourMethod(int x, int y) and int yourMethod(int x, double y).

    but can i use different types in overload?

    True or False

    hisMethod() may be overloaded as:
    int hisMethod(int x, int y) and float hisMethod(int x, int y).

    the parameters are the same so it if False, but what if the types were different does it matter?

  2. #2
    DiamondSoul is offline Member
    Join Date
    Oct 2011
    Posts
    83
    Rep Power
    0

    Default Re: question about overload

    I'm fairly sure the 2nd example you gave would not be allowed. I think that if you were to change the types of one of the variables (i.e. int hisMethod(int x,int y) and float hisMethod(int x, double y)) then that would be allowed, but I'm not entirely sure. Best thing to do would be to write a short program that uses these examples and see if they work.

  3. #3
    tripline is offline Member
    Join Date
    Oct 2011
    Posts
    19
    Rep Power
    0

    Default Re: question about overload

    we havent been using them for programming yet. We are just learning theory.

  4. #4
    tripline is offline Member
    Join Date
    Oct 2011
    Posts
    19
    Rep Power
    0

    Default Re: question about overload

    so your saying that the TYPE does not matter, all i look for is the Parameters in the method?

  5. #5
    DiamondSoul is offline Member
    Join Date
    Oct 2011
    Posts
    83
    Rep Power
    0

    Default Re: question about overload

    Well, I'm not entirely sure. When I said write a program, I didn't mean make one that actually does something. Just write a couple overloaded methods using various schemes and see whether or not your IDE complains.

    For example:

    int foo(int x){...}
    int foo(double x){...}

    This one I know is okay.



    int foo(int x){...}
    double foo(int x){...}

    This one I'm 98% sure is not okay.



    int foo(int x){...}
    double foo(double x){...}

    This one I'm 60% sure is okay (though I would rarely, if ever, actually use this in a program because it's confusing - the return type changes based on the argument type).



    Try separately putting each of these method pairs (with a "return 0;" between each pair of braces) in your Main class and see if your IDE complains.

  6. #6
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,440
    Blog Entries
    7
    Rep Power
    20

    Default Re: question about overload

    The compiler checks which one of the overloaded methods to use by inspecting the types of the parameter lists (and the number of parameters). If it can uniquely find one it automatically knows the type of the return value of that method.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  7. #7
    tripline is offline Member
    Join Date
    Oct 2011
    Posts
    19
    Rep Power
    0

    Default Re: question about overload

    Quote Originally Posted by JosAH View Post
    The compiler checks which one of the overloaded methods to use by inspecting the types of the parameter lists (and the number of parameters). If it can uniquely find one it automatically knows the type of the return value of that method.

    kind regards,

    Jos
    Im suppose to be able to figure it out without the compiler. My questions is actually what makes something overload. I believe that only the parameters determine this. Would you concur or are there other information to take into consideration as well?

  8. #8
    DiamondSoul is offline Member
    Join Date
    Oct 2011
    Posts
    83
    Rep Power
    0

    Default Re: question about overload

    Assuming I understand Jos correctly, you are correct; only the parameters determine overloading.

  9. #9
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,784
    Rep Power
    7

    Default Re: question about overload

    Quote Originally Posted by tripline View Post
    int hisMethod(int x, int y) and float hisMethod(int x, int y).
    The return type is not considered part of the method signature. The compiler has no idea which method to call as they have the same signature. "But what about the type of the variable the return value is being assigned to?" I hear you cry.
    Java Code:
    int var1 = hisMethod(1,2); // should call the int method
    float var2 = hisMethod(1,2); // should call the float method
    hisMethod(1,2);
    But what about the last line? Which method should be called?

  10. #10
    tripline is offline Member
    Join Date
    Oct 2011
    Posts
    19
    Rep Power
    0

    Default Re: question about overload

    I was just repeating the same question hoping for a yes or no answer but i get technical responses that was hard to understand in return.

    I understand what your saying now,
    But in this case

    int var1 = hisMethod(1,2);
    float var2 = hisMethod(1,2,3);

    hisMethod(1,2) //does hisMethod work or does the 3rd added index not matter because 1,2 is still the same?
    what about hisMethod(1,2,3)

  11. #11
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,784
    Rep Power
    7

    Default Re: question about overload

    If there is a method that takes 3 int parameters then obviously that is different to a method that takes only 2 int parameters. The order of the parameters also matters.
    Java Code:
    public void method(String a, int i)
    public void method(int i, String s)
    They are 2 different methods.

  12. #12
    tripline is offline Member
    Join Date
    Oct 2011
    Posts
    19
    Rep Power
    0

    Default Re: question about overload

    Thank you. I think I understand how to look to see if a method will overload without having to test it. Let me know if there is anything i should look out for.

Similar Threads

  1. Question mark colon operator question
    By orchid in forum Advanced Java
    Replies: 9
    Last Post: 12-19-2010, 08:49 AM
  2. Help with Square root function and Overload(calc error)
    By racingoutbac in forum New To Java
    Replies: 3
    Last Post: 11-04-2010, 12:09 AM
  3. Replies: 0
    Last Post: 03-31-2010, 05:18 AM
  4. Help with this code, overload the constructor
    By zoe in forum New To Java
    Replies: 12
    Last Post: 11-03-2008, 02:55 AM
  5. return type determines override/overload?
    By hedefalk in forum Advanced Java
    Replies: 4
    Last Post: 07-11-2007, 01:48 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •