aspose file tools*
The moose likes Beginning Java and the fly likes Function Overloading issues Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Function Overloading issues" Watch "Function Overloading issues" New topic
Author

Function Overloading issues

Andric Villanueva
Greenhorn

Joined: Nov 29, 2008
Posts: 6
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.


I'm getting into Java and I really love it. Especially when I compare it to C/C++ and PHP
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18987
    
    8

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

Joined: Nov 29, 2008
Posts: 6
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
Bartender

Joined: Oct 14, 2005
Posts: 18987
    
    8

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.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19783
    
  20

Or make the return type (and the entire interface) generic:


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Function Overloading issues