• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

Function overloading problem

 
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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

 
Marshal
Posts: 76876
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Marshal
Posts: 76876
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 448
Eclipse IDE Firefox Browser Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Marshal
Posts: 76876
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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?
 
Ranch Hand
Posts: 544
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 448
Eclipse IDE Firefox Browser Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Marshal
Posts: 76876
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Doody calls. I would really rather that it didn't. Comfort me wise and sterile tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic