473,385 Members | 1,562 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Error while overloading methods

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
6 1857
r035198x
13,262 8TB
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
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
praveen2gupta
201 100+
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
3,652 Expert 2GB
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
284 100+
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
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

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

Similar topics

17
by: Terje Slettebų | last post by:
To round off my trilogy of "why"'s about PHP... :) If this subject have been discussed before, I'd appreciate a pointer to it. I again haven't found it in a search of the PHP groups. The PHP...
7
by: Doran_Dermot | last post by:
Hi All, I've seen lots of code in which the attributes of a class are accessed and modified using two separate methods. For example: class Problems: def __init__( self, refNum ):...
4
by: Dave Theese | last post by:
Hello all, I'm trying to get a grasp of the difference between specializing a function template and overloading it. The example below has a primary template, a specialization and an overload. ...
31
by: | last post by:
Hi, Why can I not overload on just the return type? Say for example. public int blah(int x) { }
4
by: Doug | last post by:
Hi I am 'returning' to the learning of C# after a change in jobs and I remember that I used to struggle with the defintion, purpose and function of 'overloading'. When I was beginning with C# I...
11
by: placid | last post by:
Hi all, Is it possible to be able to do the following in Python? class Test: def __init__(self): pass def puts(self, str): print str
22
by: clicwar | last post by:
A simple program with operator overloading and copy constructor: #include <iostream> #include <string> using namespace std; class Vector { private: float x,y; public: Vector(float u, float...
2
by: rn5a | last post by:
I use VB.NET to create ASP.NET apps. If I am not wrong, there is something called method overloading in VB.NET (like in C#) which is one of the features in OOP (polymorphism) but does VB.NET also...
10
by: Matthew | last post by:
Am I correct in thinking there is no method/function overloading of any kind in any version of PHP? Thanks, Matthew
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.