Java method

A

Ah Ming

Dear all,

The following two cases are showing "same" method. If aMethod are
defined as public and private, how can I implement it? At the same
time, if both aMethod are public, how can I do? What is the
difference when they are public or they are public and private?

Kai Ming

CASE 1:

class ABC {
public int aMethod(int a) {
......
}
private void aMethod(int a) {
......
}
}

CASE 2:

class ABC {
public int aMethod(int a) {
......
}
public void aMethod(int a) {
......
}
}
 
R

Rhino

Ah Ming said:
Dear all,

The following two cases are showing "same" method. If aMethod are
defined as public and private, how can I implement it? At the same
time, if both aMethod are public, how can I do? What is the
difference when they are public or they are public and private?

Kai Ming

CASE 1:

class ABC {
public int aMethod(int a) {
......
}
private void aMethod(int a) {
......
}
}

CASE 2:

class ABC {
public int aMethod(int a) {
......
}
public void aMethod(int a) {
......
}
}

Neither of your two scenarios is possible. The documentation I have which
discusses overloading methods within the same class says:

"The overloading type of polymorphism occurs when you have the same method
names with a class. You are allowed to do this as long as the methods have
different numbers of parameters or different types of parameters. Having
different return types does not count toward 'disambiguating' methods and
won't help you if you need two methods that have the same names and
parameters but return different types."

In both of your cases, your methods have the SAME number AND type of
parameters. In Case 1, the only difference between the two methods is the
public/private and I don't see anything in the documentation that says the
Java compiler uses that to distinguish between your first 'aMethod' and your
second one. In Case 2, the return type is the only thing that distinguishes
your first 'aMethod' and your second one but the documentation clearly
states that the return type is not sufficient to distinguish between the two
methods. As a result, the Java compiler is not going to let you create
either case.

You need to change your design so that you don't need to have two methods
with exactly the same name and parameters. That shouldn't be too hard; just
add a second parameter to one of the 'aMethod's and the compiler will stop
complaining.

Rhino
 
R

Ryan Stewart

Rhino said:
Ah Ming said:
Dear all,

The following two cases are showing "same" method. If aMethod are
defined as public and private, how can I implement it? At the same
time, if both aMethod are public, how can I do? What is the
difference when they are public or they are public and private?
[...]
Neither of your two scenarios is possible. The documentation I have which
discusses overloading methods within the same class says:
[...]
As was explained by Joona some nine hours plus earlier.

OP: Please keep discussion of the same problem in the same thread. You have
now started three threads concerning the same topic.
 
A

Andrew Thompson

You have
now started three threads concerning the same topic.

There seems to be a problem with Google postings, if this one fits
the pattern,
- the OP is using Google Groups beta
- The message on post read 'could not post'
- The OP pressed 'back' then reposted it (twice, presumably)
 
T

Thomas Weidenfeller

Andrew said:
There seems to be a problem with Google postings, if this one fits
the pattern,
- the OP is using Google Groups beta
- The message on post read 'could not post'
- The OP pressed 'back' then reposted it (twice, presumably)

Google groups is switching its English version of their web interface to
a new software version, which they were running for some time now as a
separate beta version. Unfortunately, the software is still very much at
beta stage, but that doesn't prevent google from switching back and
forth between the old and the new beta version.

Many Usenet groups are now plastered with duplicates, thanks to Google
(wasn't their corporate motto "do no evil"?). The new software is broken
in other ways, too. E.g. searching for message ids does no longer work,
top level hierarchies have been renamed, the interface now requires
javascript, etc.

If you want to voice your opinion:

http://groups-beta.google.com/support/bin/request.py

/Thomas
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,281
Latest member
Pedroaciny

Latest Threads

Top