Error while overloading methods

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • 123456prakash
    New Member
    • Jan 2007
    • 35

    Error while overloading methods

    Hi all
    I have a problem.I overloaded a method .In my program i have two methods as
    given below


    [CODE=java]public Vector getList(String abc){

    Vector v = new Vector();
    // some database operation i am doing with the string abc and storing in
    // the array list // for example purpose i have added aaa,bbb,ccc

    v.add("aaa");
    v.add("bbb");
    v.add("ccc");
    return v;
    }

    public ArrayList getList(String abc){

    ArrayList av = new ArrayList();
    // some database operation i am doing with the string abc and storing in
    // the array list // for example purpose i have added aaa,bbb,ccc

    av.add("aaa");
    av.add("bbb");
    av.add("ccc");
    return av;
    }[/CODE]


    I am getting a error called duplicate method name getList()

    Is this type of overloading not possible.
    Last edited by Ganon11; Aug 16 '07, 02:21 PM. Reason: code tags added
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    Originally posted by 123456prakash
    Hi all
    I have a problem.I overloaded a method .In my program i have two methods as
    given below


    public Vector getList(String abc){

    Vector v = new Vector();
    // some database operation i am doing with the string abc and storing in
    // the array list // for example purpose i have added aaa,bbb,ccc

    v.add("aaa");
    v.add("bbb");
    v.add("ccc");
    return v;
    }

    public ArrayList getList(String abc){

    ArrayList av = new ArrayList();
    // some database operation i am doing with the string abc and storing in
    // the array list // for example purpose i have added aaa,bbb,ccc

    av.add("aaa");
    av.add("bbb");
    av.add("ccc");
    return av;
    }


    I am getting a error called duplicate method name getList()

    Is this type of overloading not possible.
    Overloading is when you have 2 or more methods in the same class with the same name but with different number and/or oder of arguments. You cannot overload on return type alone

    Comment

    • pavanuec
      New Member
      • Aug 2007
      • 3

      #3
      Originally posted by r035198x
      Overloading is when you have 2 or more methods in the same class with the same name but with different number and/or oder of arguments. You cannot overload on return type alone
      see the solution in the link
      http://j2eeinterview.b logspot.com/2007/06/1.html

      Comment

      • praveen2gupta
        New Member
        • May 2007
        • 200

        #4
        overloading methods is related with the signature ( method name + parameter sequence ) of the mathod. The return type is not the part of method overloading.

        In your case you are not following rules.

        public Vector getList(String abc)
        public ArrayList getList(String abc)

        Comment

        • Ganon11
          Recognized Expert Specialist
          • Oct 2006
          • 3651

          #5
          Basically, the reason your compiler is complaining is because it's confused. Suppose you call this method as normal:

          [CODE=java]<yourClass>.get List("A sample String.");[/CODE]

          Now, all the computer knows is that you are calling a method called getList and that you are passing it "A sample String.". It looks at its list of methods that fit this description and finds two! Now, which one should it call? The compiler has no way of telling, and determines that you cannot have this situation - if you have two functions with the same name, their argument list must differ somehow.

          Comment

          • blazedaces
            Contributor
            • May 2007
            • 284

            #6
            Its been explained enough times above why you're receiving this error. I would suggest making the two methods have two different names that lets you easily remember which one is which like these two:

            Code:
            public Vector getVector(String abc)
            public ArrayList getArrayList(String abc);
            Hope this helped and good luck,

            -blazed

            Comment

            • pavanuec
              New Member
              • Aug 2007
              • 3

              #7
              You can't overload method on the basis of return type. The reason is that sometime program calls a method for only its side effect. It may or may not use return type. As in our case we have two methods and if you call any method without caring about return value then compiler won't be able to decide which one to call..

              To read in more detail visit
              http://j2eeinterview.b logspot.com

              Comment

              Working...