Compiler error using java generics with a List as a method parameter and throws generic Exception -
i'm getting compiler error when using generics in project. generate sample code:
my bean interface
package sample; public interface mybeaninterface { long getid(); string getname(); }
my bean concrete class
package sample; public class mybean implements mybeaninterface { private long id; private string name; @override public long getid() { return id; } public void setid(long id) { this.id = id; } @override public string getname() { return name; } public void setname(string name) { this.name = name; } }
my manager interface
package sample; import java.util.list; public interface mymanagerinterface<t extends mybeaninterface> { <exception extends exception> list<t> sortall(list<t> array) throws exception; list<t> sortall2(list<t> array); <exception extends exception> list<t> sortall3() throws exception; }
my manager concrete class
package sample; import java.io.ioexception; import java.util.list; public class myconcretemanager implements mymanagerinterface<mybean> { @override //this fails public list<mybean> sortall(list<mybean> array) throws ioexception { return null; } @override //this works public list<mybean> sortall2(list<mybean> array) { return null; } @override //this works public list<mybean> sortall3() { return null; } }
i tried using method sortall no method parameters (sortall()) in interface , compiles, using exception in interface works, using both not.
thanks.
with regards sortall(list<t> list)
method, have do:
@override public <e extends exception> list<t> sortall(list<t> array) throws e { // todo auto-generated method stub return null; }
and then, when invoking method explicitly set methods type-parameter:
try { new myconcretemanager().<ioexception>sortall(...); } catch (ioexception e) { }
the sortall3()
implementation compiles fine, because in java, when method definition overrides one, not allowed throw additional checked exceptions, may throw fewer.
Comments
Post a Comment