java - Generic inheritance Issue -


i have couple interfaces support our post processing of entities:

workflowprocessor

public interface workflowprocessor {    void postprocess(list<workflowstrategy> strategies); } 

workflowaction

public class workflowaction implements workflowprocessor{    ...    ...     public void postprocess(list<workflowstrategy> strategies){       for(workflowstrategy strategy : strategies){            strategy.process(this)       }     } } 

workflowstrategy

public interface workflowstrategy {    void process(workflowprocessor itemtoprocess); } 

ticketworkflowstrategy

public class ticketworkflowstrategy implements workflowstrategy {    ...    ...    @overried    public void process(workflowaction action){   //must override or implement supertype method        // lot of processing    } } 

i'm trying figure out why cannot compile workflowaction class. works fine. thoughts on how can run correctly?

that's because you've got declare same signature method in interface:

<t extends workflowprocessor> void process(t itemtoprocess) 

declaring method in interface doesn't mean can specialize implementations of more specific parameters. method has accept any workflowprocessor.

because of fact, type variable here pretty useless: may declare in interface, makes cleaner implement too:

void process(workflowprocessor itemtoprocess); 

method-level type variables aren't useful unless doing 1 or more of following:

  • returning same type non-generic parameter
  • constraining generic parameter related either parameter or return type.

if want specialize process method particular subclass of workflowprocessor, have put on interface:

public interface workflowstrategy<t extends workflowprocessor> {   void process(t itemtoprocess); } 

then:

public class ticketworkflowstrategy implements workflowstrategy<workflowaction> {   @override   public void process(workflowaction action){     // ...   } } 

Comments