Page 1 of 2 12 LastLast
Results 1 to 20 of 23
  1. #1
    lala is offline Member
    Join Date
    Oct 2010
    Posts
    80
    Rep Power
    0

    Default Method Overloading Verification

    Hi.
    I want to make sure I have the correct understanding of Method Overloading.

    If the methods have the same name, BUT different formal parameter lists, they are method overload.
    Exs.
    Method Overload
    public int methodABC (int a, char b, double c)
    public int methodABC (char a, int b, double c)

    Not Method Overload
    If they have the same method name and the same formal lists they are NOT method overload.
    public int methodABC (int a, char b, double c)
    public int methodABC (int a, char b, double c)

    What if they had same name, & they had the same formal list/signature, but the value/return type was different
    public double methodABC (int a, char b, double c)
    public int methodABC (int a, char b, double c)

  2. #2
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    11,855
    Rep Power
    19

    Default

    Yes that's overloaded, assuming those methods are in the same class anyway, or one of them is in a subclass.

    The not overloaded methods cannot be in the same class (can't have two methods with the same signature in the same class)...so I assume you mean the second one is in a subclass? In which case that's overriding.

    And finally, a method cannot be differentiated from another method solely by its return parameter, so those two examples cannot exist together in the same class hierarchy.

  3. #3
    lala is offline Member
    Join Date
    Oct 2010
    Posts
    80
    Rep Power
    0

    Default Method Overloading Verification

    Thanks for responding Tolls.

    I think that there must be a one-to-one match between actual and formal parameters.

    If there are 4 formal, then there should be 4 actual. Is that right?

  4. #4
    KevinWorkman's Avatar
    KevinWorkman is online now Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,888
    Rep Power
    8

    Default

    What do you mean by "actual" and "formal" parameters?

  5. #5
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    11,855
    Rep Power
    19

    Default

    I was just going to ask that question...:)

    An overloaded method is a method that has the same name, but different parameters. I'm pretty sure parameter count doesn't matter.

  6. #6
    lala is offline Member
    Join Date
    Oct 2010
    Posts
    80
    Rep Power
    0

    Default Method Overloading Verification

    KevinWorkman, this is my understanding.

    Formal parameters are declared in the header:
    public double pow (double x, double y)

    Actual parameters are in the body (or when the method is called/used):
    Java Code:
    double x = 2.0;
    double y = 2.0; 
    
    z = pow(x,y) or z = pow(2.0,2.0);
    If this example is not correct, will someone please let me know.

  7. #7
    lala is offline Member
    Join Date
    Oct 2010
    Posts
    80
    Rep Power
    0

    Default Method Overloading Verification

    Tolls, this is what I read:
    "In a method call, the number of actual parameters, together with their dataa types, must match the formal parameters in the order given. That is, actual & formal parametes have a one-to-one correspondence." (Java Programming)

    I found this on Google:
    "When you call a subroutine, you must provide one actual parameter for each formal parameter in the subroutine's definition." (Java Programing: Section 4.3)

    lala

    Does anybody know for sure?

  8. #8
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    11,855
    Rep Power
    19

    Default

    That's got nothing to do with overloading or overriding though.
    That's to do with calling a method...which is why I was confused.

  9. #9
    lala is offline Member
    Join Date
    Oct 2010
    Posts
    80
    Rep Power
    0

    Default Method Overloading Verification

    I don't know, Tolls. :confused:

    Does anyone know if formal and actual parameters have to have a one-to-one match?

    Thanks In Advance

  10. #10
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    11,855
    Rep Power
    19

    Default

    Of course they do, but that has nothing to do with overloading, except in so far as the compiler using the parameters supplied in a call to determine which overloaded method to use.

  11. #11
    lala is offline Member
    Join Date
    Oct 2010
    Posts
    80
    Rep Power
    0

    Default Method Overloading Verification

    Tolls, now that I re-think this, I don't think it matters if they do not match.

  12. #12
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    11,855
    Rep Power
    19

    Default

    What?
    What are you talking about now?
    Calling a method? If so, then the parameters have to match.
    Overloading? Well, the parameters can't match.
    Overriding? Then they have to match, otherwise it's overloading.

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

    Default

    Quote Originally Posted by Tolls View Post
    I'm pretty sure parameter count doesn't matter.
    Yes it does; have a look:

    Java Code:
    public void foo(double a) { ... }
    public void foo(double a, double b) { ... }
    Two overloaded methods with a different signature (because of their different parameter count).

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  14. #14
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    11,855
    Rep Power
    19

    Default

    No, what I mean is the OP is saying something about parameter count matching...which is why I say parameter count doesn't matter. If the signature is different (whether through parameter types or number) then it's overloading.

    This is what they said:
    "I think that there must be a one-to-one match between actual and formal parameters."

    Which is untrue for overloading. The count doesn't matter, it's the difference in signature that does.

    In essence I think the OP is mixing different concepts and confusing matters.

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

    Default

    Quote Originally Posted by Tolls View Post
    No, what I mean is the OP is saying something about parameter count matching...which is why I say parameter count doesn't matter. If the signature is different (whether through parameter types or number) then it's overloading.

    This is what they said:
    "I think that there must be a one-to-one match between actual and formal parameters."

    Which is untrue for overloading. The count doesn't matter, it's the difference in signature that does.

    In essence I think the OP is mixing different concepts and confusing matters.
    Oops, sorry about the confusion I may have caused; b.t.w. overloading is a bit more complicated because of widening conversions; e.g.

    Java Code:
    public void foo(double a, int b) { ... }
    public void foo(int a, double b) { ... }
    ...
    foo(1.0, 2); // fine
    foo(1, 2.0); // also fine
    foo(1, 2); // ambiguous
    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  16. #16
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    11,855
    Rep Power
    19

    Default

    Well, yes...but there's enough confusion here without bringing that lot into it...Loretta.

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

    Default

    Quote Originally Posted by Tolls View Post
    Well, yes...but there's enough confusion here without bringing that lot into it...Loretta.
    Ooh men ... you ain't seen nothing yet! Why do you primitive species always want to keep things so simple? The answer: lack of braincells.

    kindest regards,

    Loretta ;-)
    cenosillicaphobia: the fear for an empty beer glass

  18. #18
    couling is offline Member
    Join Date
    Nov 2010
    Posts
    54
    Rep Power
    0

    Default

    FYI:

    The history behind overloading was that origionally (in older languages such as c) overloading was not possible.

    This had the outcome that (to bring it forward to a java example) you had to have functions of the form:

    Java Code:
    void printInt(int x) {...}
    void printDouble(double x) {...}
    This made the api more difficult to remember and source a little more difficult to read.

    So later languages (c++ onwards) let you give different functions the same name as long as the compiler could allways figure out which function you were trying to use. But it was put there to let you give the same name to several functions which do the same thing (eg "print" or "add").

    To that end it is usually considdered really bad practise to give the same name to two functions which do different things. Some people relax this rule a little if the different functions take a different number of arguements but the functions should have the same general purpose.

    I bring this up because I have seen people intentionally write code as mindless as:
    Java Code:
    public void doSomething(int x) {turnLeft(x);}
    public void doSomething(double x) {turnRight(x);}
    ----Signature ----
    Please use [CODE] tags and indent correctly. It really helps when reading your code.

  19. #19
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    11,855
    Rep Power
    19

    Default

    Quote Originally Posted by JosAH View Post
    Ooh men ... you ain't seen nothing yet! Why do you primitive species always want to keep things so simple? The answer: lack of braincells.

    kindest regards,

    Loretta ;-)
    Mrs Tolls says it's because we can't concentrate on more than one thing at a time.
    Which, to be fair, is not too far from the truth. Is it so bad to want to actually finish something before you get dumped with another task?

  20. #20
    lala is offline Member
    Join Date
    Oct 2010
    Posts
    80
    Rep Power
    0

    Default Method Overloading Verification‏

    Yes Tolls, and all. The OP may have been " mixing different concepts and confusing matters." :)

    Sorry about the confusion. I'm new to Java.
    Many thanks to Everyone for your contributions.

    :)

Page 1 of 2 12 LastLast

Similar Threads

  1. Overloading qn
    By diskhub in forum New To Java
    Replies: 18
    Last Post: 06-07-2010, 05:19 PM
  2. method overloading and arrays.
    By glopez09 in forum New To Java
    Replies: 5
    Last Post: 12-08-2009, 03:02 PM
  3. Replies: 0
    Last Post: 04-17-2008, 07:43 PM
  4. Overloading?
    By padutch2 in forum New To Java
    Replies: 2
    Last Post: 12-31-2007, 03:26 AM
  5. Method/Operator Overloading
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 12-01-2007, 08:33 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
  •