java - Can interface methods be overloaded? -
i'm trying achieve overloaded interface method. know not work in java, how rewrite following have implementation type in action()
methods, , not base
type?
class base; class foo extends base; class bar extends base; interface iservice { void action(base base); } class fooservice implements iservice { void action(foo foo) { //executes specific foo action } } class barservice implements iservice { void action(bar bar) { //executes specific bar action } }
usage:
base base; //may foo or bar anyservice.action(bar);
you idea. how this?
define interface both foo
, bar
should implement, can like:
interface actionable{ public void action; } class base; class foo extends base implements actionable; class bar extends base implements actionable; interface iservice { void action(actionable a); } class fooservice implements iservice { void action(actionable a) { ... } } class barservice implements iservice { void action(actionable a) { ... } }
anyway interfaces should make code more robust , reusable - if looking hacks make them work, consider designing application better.
Comments
Post a Comment