1 2 Previous Next 16 Replies Latest reply: Feb 5, 2011 3:51 AM by EJP RSS

    A question about overloading methods

    763890
      Hi, there!

      I have found this interesting fact about the concept of method signature when it comes to overloading methods.
      Due to the Java Tutorials,*...Overloaded methods are differentiated by the number and the type of the arguments passed into the method....*;however, we can also overload methods with the same number but different type of the arguments by the order of the arguments in the parameter list of the method signature.

      And here is what I meant:
      class Example {
      void printData(int iNum, double dNum) {;}
      void printData(double dNum, int iNum){;}
      }
      As shown above, I can't understand why Java would allow this kind of overloading without throwing compile-time error. Can anybody tell me why?

      Well, since it compiles without any errors but it is not recommended to use it this way;otherwise, it would make our code difficult to understand. Is it right?
        • 1. Re: A question about overloading methods
          Kayaman
          HappyJay wrote:
          As shown above, I can't understand why Java would allow this kind of overloading without throwing compile-time error. Can anybody tell me why?
          I guess they forgot to specify that the order of the parameter matters as well. I don't understand why you seem so shocked.
          Well, since it compiles without any errors but it is not recommended to use it this way;otherwise, it would make our code difficult to understand. Is it right?
          Well, your example is bad because your parameters are named "iNum" and "dNum" which have no meaning. In that case yes, I'd very much avoid writing anything like your example.

          However, if we take a bit more complicated example, such as
          public void printAnimal(Animal animal, int age, boolean printFullName) {
              System.out.println("I'm a " + printFullName ? animal.getFullName() : animal.getName() + " and I'm " + age + " years old!");
          }
          public void printAnimal(int numberOfAnimals, Animal animal, boolean printFullName) {
              System.out.println("We're a pack of " + printFullName ? animal.getFullName() : animal.getName() + "s and there's " + numberOfAnimals + " of us!");
          }
          You can see a difference. Of course that would still be bad design to have an overloaded method instead of a proper method for displaying animal packs.
          But there's a distinct difference to having just "iNum" and "dNum" as parameters.

          Also, did you know that a group of crows is a murder, and a group of jellyfish is known as a smack?
          Now you do.
          • 2. Re: A question about overloading methods
            jschellSomeoneStoleMyAlias
            HappyJay wrote:
            As shown above, I can't understand why Java would allow this kind of overloading without throwing compile-time error. Can anybody tell me why?
            First step understand that the Java tutorials have nothing to do with your question. "Java" is defined by a number of specifications. The relevant one in this case would be the following.

            http://java.sun.com/docs/books/jls/third_edition/html/j3TOC.html

            From that overloading is defined by the method signature. And that is defined by the following.

            http://java.sun.com/docs/books/jls/third_edition/html/classes.html#8.4.2

            The third bullet point describes parameters and specifies ordering by symbolic convention.

            However in computer languages that use methods argument ordering is always relevant unless a mechanism exists to provide parameter naming for passed values. That is necessary to implement the calling convention.
            Well, since it compiles without any errors but it is not recommended to use it this way;otherwise, it would make our code difficult to understand. Is it right?
            There are all sorts of ways to write code that is difficult to maintain, hard to understand, etc. Excessive overloading could lead to that sort of problem. For your given example probably the only acceptable version would be the following. It is acceptable because it preserves the functional meaning of what the method 'printDate' regardless of which method is called.
            void printData(int iNum, double dNum) {;}
            void printData(double dNum, int iNum){  printData(iNum, dNum);   }
            • 3. Re: A question about overloading methods
              YoungWinston
              Kayaman wrote:
              Also, did you know that a group of crows is a murder
              And a group of ravens is an 'unkindness'.

              Winston
              • 4. Re: A question about overloading methods
                763890
                Kayaman wrote:
                I guess they forgot to specify that the order of the parameter matters as well. I don't understand why you seem so shocked.
                Oh, that's interesting! BTW, I wasn't shocked but confused by what I found.

                >
                Well, your example is bad because your parameters are named "iNum" and "dNum" which have no meaning. In that case yes, I'd very much avoid writing anything like your example.

                However, if we take a bit more complicated example, such as
                My sample code might not be the best example by "your standard";however, as long as it proves the point and meets the SSCCE then it's good enough.
                Also, did you know that a group of crows is a murder, and a group of jellyfish is known as a smack?
                Now you do.
                What are these to do with the question that I have asked?
                Frankly speaking, no I don't.
                • 5. Re: A question about overloading methods
                  763890
                  Thanks for pointing out the exact links of the JLS, where the answer to my question resides.
                  • 6. Re: A question about overloading methods
                    Kayaman
                    HappyJay wrote:
                    My sample code might not be the best example by "your standard";however, as long as it proves the point and meets the SSCCE then it's good enough.
                    I don't have any standards concerning the order of parameters. I just showed some code where the switched order of parameters is a little less unclear than in your example with the generic parameter names. Although admittedly it's still not clear code and shouldn't be used.
                    What are these to do with the question that I have asked?
                    Frankly speaking, no I don't.
                    They're bonus knowledge. Fix your attitude, bub.
                    • 7. Re: A question about overloading methods
                      763890
                      Kayaman wrote:
                      They're bonus knowledge. Fix your attitude, bub.
                      I don't think there is anything wrong with my attitude.

                      I just don't take them as "bonus knowledge" because you haven't given any explanation of the meanings. Therefore, I took them as a humiliation instead at the first place.

                      If you would like to give out extra information for something else on a side note, please make it crystal clear to your reader that they are bonus knowledge.

                      Anyway, my purpose is to learn more about Java, not to argue or run into any kind of dispute with someone.

                      If what I have written make you feel insulted, I am sorry because I have no intentions of doing that.
                      • 8. Re: A question about overloading methods
                        804650
                        YoungWinston wrote:
                        Kayaman wrote:
                        Also, did you know that a group of crows is a murder
                        And a group of ravens is an 'unkindness'.

                        Winston
                        A crash of rhinoceroses.
                        • 9. Re: A question about overloading methods
                          EJP
                          As shown above, I can't understand why Java would allow this kind of overloading without throwing compile-time error.
                          Compile-time errors are not thrown. Exceptions are thrown. This is not an exception. It is a compilation error.
                          Can anybody tell me why?
                          Because it is legal Java. Because it is built into the language. Because they decided to do so. I don't know why you think this is so startling. It's been there for over 15 years. C++ has had it for over 25 years.
                          Well, since it compiles without any errors but it is not recommended to use it this way;otherwise, it would make our code difficult to understand. Is it right?
                          No it is not right. It is a language feature. Like all language features, it can be used or abused. It's up to you.
                          • 10. Re: A question about overloading methods
                            Kayaman
                            HappyJay wrote:
                            I just don't take them as "bonus knowledge" because you haven't given any explanation of the meanings. Therefore, I took them as a humiliation instead at the first place.
                            Maybe you shouldn't take things immediately in a negative way. You know, give the benefit of the doubt. There are so many people here and many of them aren't native English speakers (me included), that confusions in posts are bound to happen.
                            Anyway, my purpose is to learn more about Java, not to argue or run into any kind of dispute with someone.
                            Well, I think most of us are here to learn more about Java, but there's plenty of arguments and disputes here too..
                            If what I have written make you feel insulted, I am sorry because I have no intentions of doing that.
                            No worries.
                            • 11. Re: A question about overloading methods
                              YoungWinston
                              Pinto wrote:
                              A crash of rhinoceroses.
                              Actually, Wikipedia lists it as a 'bloat' :-).

                              Winston
                              • 12. Re: A question about overloading methods
                                763890
                                EJP wrote:
                                Compile-time errors are not thrown. Exceptions are thrown. This is not an exception. It is a compilation error.
                                Sorry, I meant to use the word "throw" to refer to the action of Java compiler recognizes it as a grammatical incorrectness and output as a compilation error but not to refer to the concept of programming language itself.
                                Sorry for the misunderstanding.
                                Because it is legal Java. Because it is built into the language. Because they decided to do so. I don't know why you think this is so startling. It's been there for over 15 years. C++ has had it for over 25 years.
                                That's a interesting fact. Thanks! It's good to know it;however, I wonder why such a important bit of fact isn't mentioned at all in Java Tutorial. I think it's quite often being ignored by newbie Java programmers like me, who don't have any previous experience with C++ and so forth.
                                No it is not right. It is a language feature. Like all language features, it can be used or abused. It's up to you.
                                Great! Thanks for your clarification.
                                • 13. Re: A question about overloading methods
                                  763890
                                  Kayaman wrote:
                                  Maybe you shouldn't take things immediately in a negative way. You know, give the benefit of the doubt. There are so many people here and many of them aren't native English speakers (me included), that confusions in posts are bound to happen.
                                  Yep, I do agree with you. Well, I wasn't take things negatively by all means.
                                  I use to tend to expect a slightly gentle way of expression as a response. That's all.
                                  Well, I think most of us are here to learn more about Java, but there's plenty of arguments and disputes here too..
                                  Right! There is no doubt.
                                  • 14. Re: A question about overloading methods
                                    796440
                                    HappyJay wrote:
                                    EJP wrote:
                                    Because it is legal Java. Because it is built into the language. Because they decided to do so. I don't know why you think this is so startling. It's been there for over 15 years. C++ has had it for over 25 years.
                                    That's a interesting fact. Thanks! It's good to know it;however, I wonder why such a important bit of fact isn't mentioned at all in Java Tutorial.
                                    http://download.oracle.com/javase/tutorial/java/javaOO/methods.html, under "Overloading Methods" says, "...methods within a class can have the same name if they have different parameter lists..." Now, while it doesn't explicitly state that (int, double) is a different parameter list than (double, int), if I read that for the first time, I would guess that it probably was, and then I'd probably test it to clear up the ambiguity.

                                    So it is mentioned in the tutorial. Just not spelled out quite as explicitly as you might have wanted.
                                    1 2 Previous Next