Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

A mystery with methods. 1

Status
Not open for further replies.

ACRRHODES

Programmer
May 16, 2000
33
CA
I know that it is possible to do the following:
[tt]
public void method(int int_value)
{
//...
}

public void method(float float_value)
{
//...
}

public void method(String string_value)
{
//...
}
[/tt]
And-so-on-and-so-forth. But is it possible to do this:
[tt]
public int method()
{
//...
}

public float method()
{
//...
}

public String method()
{
//...
}
[/tt]
If so, when you were to, for example, write:
[tt]
float x;
x = method();
[/tt]
Would the compiler automatically run the 'float' version of the method? What if it was less clear, i.e.
[tt]
System.out.println("Good morning Dave, this is " + method() + " speaking.");
[/tt]
Would method() return 42, 3.14, or "HAL"?
Hmm... a mystery! [pc3] ***
Dammit Jim, I'm a programmer, not a doctor.
 
Another mystery!:)
Consider the following:
[tt]
public void method()
{
//...
}

private void method()
{
//...
}
[/tt]
If you were to call [tt]method()[/tt], which method would run? Would it be possible to run the other one? ***
Dammit Jim, I'm a programmer, not a doctor.
 
These things are NOT possible, there is no mystery. Each method must have a distinct signature. A method's signature is comprised of its name and ordered parameter list.

Therefore the following will not compile:
Code:
public class Test {
  public int method(int input) { return 1; }
  private float method(int inputNumber) { return 1.1F; }
}
This will result in a compilation error because more than one method has a signature of method(int). To reiterate, accessibility modifiers and return values are not part of a method's signature.
 
The in paramenters alone can be different. You cannot have overloaded methods with various return type alone.
Method overload basically talks about only the in parameters to the method.

mesuj
 
Is there a 'Mixed' data type in Java? Is it possible to return different types of data from a single method, or would you have to do this:
[tt]
class Mixed
{
int i;
double d;
String s;
}

class Other
{
Mixed m = new Mixed();

Mixed method(int i)
{
Mixed ret = new Mixed;
ret.i = i;
retrun ret;
}

Mixed method(double d)
{
Mixed ret = new Mixed;
ret.d = d;
retrun ret;
}

Mixed method(String s)
{
Mixed ret = new Mixed;
ret.s = s;
retrun ret;
}
[/tt]

You know, that sort of thing. ***
Dammit Jim, I'm a programmer, not a doctor.
 
You can use Object. That will allow multiple data type returns.

Then for double, you use Double and so on.... Mike Wills
AS400 Programmer
[pc2]

Please, if you find my post useful, let me know. [thumbsup2]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top