Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Function Overloading issues

 
Andric Villanueva
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • 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.
 
Paul Clapham
Sheriff
Posts: 21298
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • 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
  • 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
Sheriff
Posts: 21298
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • 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.
 
Rob Spoor
Sheriff
Pie
Posts: 20605
60
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Or make the return type (and the entire interface) generic:
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic