1 2 Previous Next 16 Replies Latest reply: Nov 9, 2005 9:26 AM by 807597 RSS

    ----- >  Method  Overloading  Question < -----

    807597
      overloading requires methods with distinct signatures. The signature of a method includes its name and the ordered list of its argument types. all other items appearing in a method header, such as exceptions, return type, final, and synchronized, do not contribute to a method's signature......

      please correct me if i'm wrong, does it mean that:

      (1) the list of argument types (int, int, float) must be in different order like (int, float, int) , (float, int, int) , (int, int), (int, float) ... in order to overload?
      (2) the list of arguments (d, e, f) can be in any order like (d,e,z) , (a, b),(dx,ey,fz) or anything else?

      Take the following case as an example,
      public class MethodOverload{
        private int x, y;
        private float z;
      
        public void A(int d, int e, float f){
          x = d;
          y = e;
          z = f;
      }
      Can A method be overloaded as below?
      public void A(int dx, int ey, float fz){
        x = dx;
        y = ey;
        z = fz;
      }
        • 1. Re: ----- >  Method  Overloading  Question < -----
          807597
          overloading requires methods with distinct
          signatures. The signature of a method includes its
          name and the ordered list of its argument types.
          all other items appearing in a method header, such
          ch as exceptions, return type, final, and
          synchronized, do not contribute to a method's
          signature......

          please correct me if i'm wrong, does it mean that:

          (1) the list of argument types (int, int, float) must
          be in different order like (int, float, int) ,
          (float, int, int) , (int, int), (int, float) ... in
          order to overload?
          (2) the list of arguments (d, e, f) can be in any
          order like (d,e,z) , (a, b),(dx,ey,fz) or anything
          else?

          Take the following case as an example,
          public class MethodOverload{
          private int x, y;
          private float z;
          
          public void A(int d, int e, float f){
          x = d;
          y = e;
          z = f;
          }
          Can A method be overloaded as below?
          public void A(int dx, int ey, float fz){
          x = dx;
          y = ey;
          z = fz;
          }
          no, argument names are irrelevant, only types matter
          • 2. Re: ----- >  Method  Overloading  Question < -----
            807597
            > public class MethodOverload{
            
            private int x, y;
            private float z;

            public void A(int d, int e, float f){
            x = d;
            y = e;
            z = f;
            }
            Can A method be overloaded as below? > public void A(int dx, int ey, float fz){
            x = dx;
            y = ey;
            z = fz;
            }
            In your example you're not using overloading but overriding
            but IMHO you can't do this in the same class. You can only use overriding on a father class' method or on an interface's method.
            To be overload it should be something like:
            public void A(int x){
                         ...//method code
            }
            or:
             public int void A(int d, int e, float f){
                 x = d;
                 y = e;
                 z = f;
                 return y;//just an example...
             }
            • 3. Re: ----- >  Method  Overloading  Question < -----
              807597
              ops... -_-' 
              little syntax error in my example... it should have been:
                public int A(int d, int e, float f){
                    x = d;
                    y = e;
                    z = f;
                    return y;//just an example...
                }
              sorry ;-)
              • 4. Re: ----- >  Method  Overloading  Question < -----
                807597
                ops... -_-' 
                little syntax error in my example... it should have
                been:
                public int A(int d, int e, float f){
                x = d;
                y = e;
                z = f;
                return y;//just an example...
                }
                sorry ;-)
                why did you take off void instead?
                • 5. Re: ----- >  Method  Overloading  Question < -----
                  807597
                  ops... -_-' 
                  little syntax error in my example... it should
                  have
                  been:
                  public int A(int d, int e, float f){
                  x = d;
                  y = e;
                  z = f;
                  return y;//just an example...
                  }
                  sorry ;-)
                  why did you take off void instead?
                  'cause you can't write a method that returns two type. and void already was in the original example, so I changed it to int.
                  • 6. Re: ----- >  Method  Overloading  Question < -----
                    807597
                    i thought the only limitation for overloading is the order list of its arguments must be unequal. others does not matter. isn't it?
                    • 7. Re: ----- >  Method  Overloading  Question < -----
                      807597
                      I'm pretty sure you can also change the return type.
                      • 8. Re: ----- >  Method  Overloading  Question < -----
                        807597
                        oh, i see. my mistakes! sorry too :-)
                        • 9. Re: ----- >  Method  Overloading  Question < -----
                          807597
                          thanks for your example ~
                          • 10. Re: ----- >  Method  Overloading  Question < -----
                            JosAH
                            thanks for your example ~
                            Maybe some other examples may help too:
                            // properly overloaded methods:
                            int foo(int x, int y);
                            int foo(int x); // parameters type list differs: ok
                            int foo(int x, int y, int z); // ditto
                            int foo(double d); // ditto
                            int bar(int x, int y); // method name differs, no overloading but legal
                            // not valid:
                            double foo(int x, int y); // param type list equals; return type doesn't count
                            int foo(int x, int y); // identical to first method: not allowed
                            kind regards,

                            Jos
                            • 11. Re: ----- >  Method  Overloading  Question < -----
                              807597
                              thanks for your example ~
                              Maybe some other examples may help too:
                              // properly overloaded methods:
                              int foo(int x, int y);
                              int foo(int x); // parameters type list differs: ok
                              int foo(int x, int y, int z); // ditto
                              int foo(double d); // ditto
                              int bar(int x, int y); // method name differs, no
                              overloading but legal
                              // not valid:
                              double foo(int x, int y); // param type list equals;
                              return type doesn't count
                              int foo(int x, int y); // identical to first method:
                              not allowed
                              kind regards,

                              Jos
                              those are very good examples, thanks! Jos
                              • 12. Re: ----- >  Method  Overloading  Question < -----
                                800322
                                i thought the only limitation for overloading is the
                                order list of its arguments must be unequal. others
                                does not matter. isn't it?
                                The list of argument types must be unequal.
                                void foo(int a, int b)
                                void foo(int b, int a)
                                can't work - if I do:
                                foo(0, 0);
                                which one should be called?
                                • 13. Re: ----- >  Method  Overloading  Question < -----
                                  807597
                                  Re: ----- > Method Overloading Question < -----
                                  Author: elektra5566 Posts: 67 Registered: 6/7/05
                                  > Nov 6, 2005 3:56 AM (reply 6 of 12)
                                  >
                                  >
                                  i thought the only limitation for overloading is the order list of its >arguments must be unequal. others does not matter. isn't it?
                                  >
                                  >
                                  Re: ----- > Method Overloading Question < -----
                                  Author: JoYsTiCk Posts: 35 Registered: 8/30/05
                                  > Nov 6, 2005 3:57 AM (reply 7 of 12)
                                  >
                                  >
                                  I'm pretty sure you can also change the return type.
                                  ehm... I'm really really sorry, but that was my mistake! You were right, actually you can change the return type but you have to change the arguments probably I was asleep while writing that!! :-(

                                  I'm really sorry,
                                  • 14. Re: ----- >  Method  Overloading  Question < -----
                                    807599
                                    quesion:

                                    then could i overload in this way??
                                    int doRead(String str){
                                    ...
                                    }
                                    
                                    String doRead(String str){
                                    ::::
                                    }
                                    It seems not to work by me.
                                    1 2 Previous Next