• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Function Overloading issues

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm having some really odd problems with function overloading that I figured wouldn't be an issue.

For the sake of brevity I moved everything into this one code block and removed all comments and package/import statements.



The issue that I'm having is that I get a compiler error on "System.out.println(s.doWork(Main.spool()));"

The reason is taht doWork is specified to take either an Ajax/Download/Managed type but the return type of Main.spool() is of the subtype.

I thought that it would take the most specified return type for the overloaded functions. How do I do this w/o having to do some crazy if/switch statement?

Sorry if this is really silly but this is how I thought it would work.

Cheers.
 
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I would say your design is wrong. If you have something which should work differently for each subtype of a certain parent, then that's a sure sign you should be using inheritance and polymorphism. In other words, it's the Renderable class (or interface?) which should declare the doWork() method and each of the subtypes should implement that method in the way appropriate for that subtype.
 
Andric Villanueva
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Paul Clapham wrote:I would say your design is wrong. If you have something which should work differently for each subtype of a certain parent, then that's a sure sign you should be using inheritance and polymorphism. In other words, it's the Renderable class (or interface?) which should declare the doWork() method and each of the subtypes should implement that method in the way appropriate for that subtype.


I would agree but at some point all of these types will have to converge. Some will turn into strings and others will be turned into binary that a user can download. So I'm not sure as to how to get that piece to work. Design-wise that is.
 
Paul Clapham
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I don't really understand what that means, so I don't think I can be of much help. All I can say at this point is that perhaps you shouldn't have the doWork() method returning anything, since you don't have an agreed-upon type for what it might return.
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Or make the return type (and the entire interface) generic:
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic