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.
6 1857
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
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
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)
Basically, the reason your compiler is complaining is because it's confused. Suppose you call this method as normal: - <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.
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: -
public Vector getVector(String abc)
-
public ArrayList getArrayList(String abc);
-
Hope this helped and good luck,
-blazed
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
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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 ):...
|
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. ...
|
by: |
last post by:
Hi,
Why can I not overload on just the return type?
Say for example.
public int blah(int x)
{
}
|
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...
|
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
|
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...
|
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...
|
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
|
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...
|
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...
|
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...
|
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...
|
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,...
|
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$) {
}
...
|
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...
|
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
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
| |