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 of rescheduleinfo, 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

Popular posts from this blog

Magento/PHP - Get phones on all members in a customer group -

php - Bypass Geo Redirect for specific directories -

php - .htaccess mod_rewrite for dynamic url which has domain names -