Board index » jbuilder » Overloading methods?

Overloading methods?


2004-02-08 10:05:26 AM
jbuilder20
i want to overload a method:
public final boolean p()
public final String p()
public final long p()
public final void p()
But the compiler tells me:
p() is already defined in ...
Now some of you might want to come back at me with: "But how can the
compiler know which method you want to call when the all have identical
parameter lists?"
Well, when the call is:
if p() then
or
if p() == 'JBuilder roxxors when it compiles'
or
p();
or
String szText = p();
It begins to get pretty obvious.
So, i assume i am missing a compiler option somewhere....
 
 

Re:Overloading methods?

"Ian Boyd" < XXXX@XXXXX.COM >writes:
Quote
public final boolean p()
public final String p()
public final long p()
public final void p()
Methods cannot be overridden with different return types.
Quote
So, i assume i am missing a compiler option somewhere....
No, what you want to do is against the language specification.
 

Re:Overloading methods?

As Tor says, what you are asking is against the language specification.
Here are some other examples in line with the ones that you mentioned:
String s = "This is a concatenated string " + p();
Now the question, is, which p()? The compiler concatenates strings for you
by doing all the necessary toString() conversions; the "String p()"
selection is only slightly more correct than the others, but not much so.
Your example:
if p() == 'JBuilder roxxors when it compiles'
is not correct. You can't compare strings for equality this way; all you're
comparing is that the two strings share the same address space, something
that is seldom going to be the case. Your example should be written as:
if (p().equals("JBuilder roxxors when it compiles"))
but then you run into problems with methods like "SomeClass p()", which
would be as good a choice for the above example as the String version.
There is an enhancement request for Java to support return type overloading
as follows:
class Base {
// Get a generic map.
public Map getAMap() {
...
}
}
class Derived extends Base {
// Get a specific type of map required for this class.
public HashMap getAMap() {
...
}
}
As long as the return type in the derived class is assignment-compatible
with the return type in the base class, the overloading would be legal. I
don't know what the status of this request is as I don't follow the language
specifications very closely but that's as good as you're ever going to get
with return type overloading.
--
Check out our latest white papers at
www.datadevelopment.com/papers/index.html
BladeNET Scores With Borland Enterprise Tools
Team Development with JBuilder and Borland Enterprise Server
Dolphin Data Development Ltd.
www.datadevelopment.com/
"Ian Boyd" < XXXX@XXXXX.COM >wrote in
message news:40259964$ XXXX@XXXXX.COM ...
Quote
i want to overload a method:

public final boolean p()
public final String p()
public final long p()
public final void p()

But the compiler tells me:
p() is already defined in ...

Now some of you might want to come back at me with: "But how can the
compiler know which method you want to call when the all have identical
parameter lists?"

Well, when the call is:
if p() then
or
if p() == 'JBuilder roxxors when it compiles'
or
p();
or
String szText = p();

It begins to get pretty obvious.

So, i assume i am missing a compiler option somewhere....


 

{smallsort}