| Author |
Function overloading problem
|
Kowshik Nandagudi
Ranch Hand
Joined: Dec 09, 2010
Posts: 57
|
|
Hi,
I am facing a problem with function overloading.
As we know we cannot overload functions based only on return type.
Example:
public String parse(Collection<LabelItem> items, TokenStack tokens)
public Double parse(Collection<LabelItem> items, TokenStack tokens)
I can return Object type here (One parse method ) and based on instance i can typecast the result. But which I dont want to do
But is there any other way I can achive this.
My Main aim is to have two seperate methods
|
 |
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32694
|
|
Not "function" please, but "method".
No, you can't overload methods like that. You would have to give them different names. They do different things, so it would be more appropriate to call them parseCollectionToString or parseCollectionToDouble or similar. And do you mean to return a double or a Double?
|
 |
Kowshik Nandagudi
Ranch Hand
Joined: Dec 09, 2010
Posts: 57
|
|
Thanks Campbell.
The method parse at the end typecasts it . Only thing i am worried is about the duplication of code. There is one possibility to move the common things to one method.
But is there any other way.
Also does duplication of the code affect the performance ?
Thanks in advance
|
 |
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32694
|
|
It is difficult to be certain; I haven't seen your code, but . . .
I don't think duplication will affect performance.
If you are casting the output to different types, you can't regard those methods as overloaded because they have the same signature and different return types.
Pulling out common code into a third method sounds a very good idea.
|
 |
Sunny Bhandari
Ranch Hand
Joined: Dec 06, 2010
Posts: 446
|
|
Kowshik Nandagudi wrote:Thanks Campbell.
The method parse at the end typecasts it . Only thing i am worried is about the duplication of code. There is one possibility to move the common things to one method.
But is there any other way.
Yes you can have one additional parameter being passed to parse method (say boolean), to indicate the instance to be returned.
But again that will cause problem if at a later stage you want to return a Long from the same method. (though you can use String as the third argument in that case)
Kowshik Nandagudi wrote:
Also does duplication of the code affect the performance ?
Thanks in advance
Yes when class is loaded, the methods are also loaded into the memory by JVM.
|
 |
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32694
|
|
Sunny Bhandari wrote: . . . Yes you can have one additional parameter being passed to parse method (say boolean), to indicate the instance to be returned. . . .
Look at methods like List#toArray(T[]) and see how they use generics to alter the return type. You might be able to use a generic method, which I really ought to have thought of earlier. Better approach than using a boolean for return type.
. . . Yes when class is loaded, the methods are also loaded into the memory by JVM.
Do you think that will make a noticeable difference to performance?
|
 |
amit punekar
Ranch Hand
Joined: May 14, 2004
Posts: 488
|
|
Hi,
Talking about the common method I think it would be better if you have something like this
Common method - parseCollection() - this has the logic to parse the collection passed as input.
Specific methods - parseCollectionToString() and parseCollectionToDouble() that just typecasts the result returned from parseCollection() and return back the specific type.
This will also help you in future in case you need to extend the parsing to different data types.
Regds,
Amit
|
 |
Sunny Bhandari
Ranch Hand
Joined: Dec 06, 2010
Posts: 446
|
|
yes use the generic method if you can. I am not sure about JRE on which your project may run in future.
Just make sure everyone uses JRE 1.5+ when running your application...
boolean argument and common method approaches are suited for languages like JavaScript.
|
 |
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32694
|
|
amit punekar wrote: . . .
Common method - parseCollection() . . .
Specific methods - parseCollectionToString() and parseCollectionToDouble() . . .
You might have the "common" method as a private method, called by the other two.
|
 |
 |
|
|
subject: Function overloading problem
|
|
|