java - Implements interface with generics, both methods have same erasure -
here's code. i've base interface define common function.
interface baseinterface<t> { void basefunc(t param) } interface myinterface1 extends baseinterface<myentity1> { void func1() } interface myinterface2 extends baseinterface<myentity2> { void func2() }
and class is:
abstract class baseclass implements baseinterface<t> { // ...some common things } class classa extends baseclass implements myinterface1 { @override public void func1() {...} @override public void basefunc(myentity1 param) {...} } class classb extends baseclass implements myinterface2 { @override public void func2() {...} @override public void basefunc(myentity2 param) {...} }
different class do same things different bean myentity1 , myentity2. defined basefunc() generic t.
but there's problem here. "both methods have same erasure yet neither overrides other basefunc(t) , basefunc(myentity1)..."
how slove problem?
by way, works fine when removed generic , move basefunc myinterface1 , myinterface2. don't think it's best way.
the declaration
abstract class baseclass implements baseinterface<t>
makes no sense, unless t
actual class. if it's implementing generic interface, needs either
- be generic class, or
- supply type argument in place of generic type parameter.
i believe wanted write was
abstract class baseclass<t> implements baseinterface<t>
to keep baseclass
generic class. declare other classes as
class classa extends baseclass<myentity1> implements myinterface1
and
class classb extends baseclass<myentity2> implements myinterface2
then there no clash of types passed basefunc
method.
Comments
Post a Comment