java - How to implement interface that uses templates? -


i'm expected implement immutable list interface looks this:

public interface interflist<t> extends iterable<t> {     public interflist<t> append(t t);     //more abstract methods follow } 

when i've let netbeans implement interface me, appeared:

public class mylist implements interflist  {   private object value;   @override   public interflist append(object t) {       throw new unsupportedoperationexception("not supported yet.");   } } 

however, want keep template proper type of object accepted. eg.:

public class mylist<t> implements interflist  {   private t value;   @override   public interflist<t> append(t t) {       throw new unsupportedoperationexception("not supported yet.");   } } 

the first approach allow kinds objects stored in list making quite mess.

the first approach, however, marked error in netbeans:

mylist not abstract , not implement method append(object) in interflist

you should do:

public class mylist<t> implements interflist<t>  {    private t value;    @override   public interflist<t> append(t t) {       throw new unsupportedoperationexception("not supported yet.");   } } 

you want implement interflist<t>.

since implementing interflist without type parameters, you're implementing interflist<object>, causes error append method, takes t instead of object. want implement interflist<t>.


Comments

Popular posts from this blog

database - VFP Grid + SQL server 2008 - grid not showing correctly -

jquery - Set jPicker field to empty value -

.htaccess - htaccess convert request to clean url and add slash at the end of the url -