By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,538 Members | 1,130 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,538 IT Pros & Developers. It's quick & easy.

Error while overloading methods

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


Expand|Select|Wrap|Line Numbers
  1. public Vector getList(String abc){
  2.  
  3.       Vector v = new Vector();
  4.  // some database operation i am doing with the string abc and storing in 
  5. // the array list  // for example purpose i have added aaa,bbb,ccc
  6.  
  7.       v.add("aaa");
  8.       v.add("bbb");
  9.       v.add("ccc");
  10.       return v;
  11. }
  12.  
  13. public ArrayList getList(String abc){
  14.  
  15.       ArrayList av = new ArrayList();
  16.  // some database operation i am doing with the string abc and storing in 
  17. // the array list  // for example purpose i have added aaa,bbb,ccc
  18.  
  19.       av.add("aaa");
  20.       av.add("bbb");
  21.       av.add("ccc");
  22.       return av;
  23. }

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

Is this type of overloading not possible.
Jan 17 '07 #1
Share this Question
Share on Google+
6 Replies


10K+
P: 13,264
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
Jan 17 '07 #2

P: 3
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.blogspot.com/2007/06/1.html
Aug 16 '07 #3

100+
P: 201
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)
Aug 16 '07 #4

Ganon11
Expert 2.5K+
P: 3,652
Basically, the reason your compiler is complaining is because it's confused. Suppose you call this method as normal:

Expand|Select|Wrap|Line Numbers
  1. <yourClass>.getList("A sample String.");
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.
Aug 16 '07 #5

blazedaces
100+
P: 284
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:

Expand|Select|Wrap|Line Numbers
  1. public Vector getVector(String abc)
  2. public ArrayList getArrayList(String abc);
  3.  
Hope this helped and good luck,

-blazed
Aug 16 '07 #6

P: 3
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.blogspot.com
Aug 18 '07 #7

Post your reply

Sign in to post your reply or Sign up for a free account.