Overridding a generic method in java -
i have method defined in 1 abstract class follows,
public abstract class abstractservice { protected abstract <t> message<t> executeservice(message<t> msg) throws serviceexception; }
i want implement method in concrete subclass follows,
public class rescheduleinfoadaptor extends abstractservice { @override protected message<rescheduleinfo> executeservice(message<rescheduleinfo> msg) throws serviceexception { //implementation goes here } }
but compiler reporting error saying
the method executeservice(message<rescheduleinfo>) of type rescheduleinfoadaptor must override or implement supertype method
can suggest how can implement super class method in child class required type?
you have 2 options:
either
- up-bound abstract method type parameter
rescheduleinfo
(or specific superclass ofrescheduleinfo
,abstractinfo
)
for example:
protected abstract <t extends rescheduleinfo> message<t> executeservice(message<t> msg) throws serviceexception;
however, note compile, generate "type safety" warning.
or
- make
abstractservice
generic , remove abstract method type-parameter.
for example:
public abstract class abstractservice<t> { protected abstract message<t> executeservice(message<t> msg) throws serviceexception; }
then, in other class, you'll have:
public class rescheduleinfoadaptor extends abstractservice<rescheduleinfo> { @override protected message<rescheduleinfo> executeservice(message<rescheduleinfo> msg) throws serviceexception { //implementation goes here } }
Comments
Post a Comment