implementing a java interface and using generics -
i have java interface called bst (short binary search tree) has generic types key,value key extends comparable.i defined below.
public interface bst<key extends comparable<key>,value> { public void put(key key,value value); public value get(key key); public void delete(key key); public iterable<key> keys(); } now want define implementation of above interface.i tried this
public class bstimpl<key extends comparable<key> ,value> implements bst<key extends comparable<key>,value> { ... } the above definition causes error message in eclipse ide ..the extends token after implements bst<key seems culprit
syntax error on token "extends", , expected
if omit "extends" token definition (as given below),the error goes away, , can eclipse generate unimplemented methods correctly
public class bstimpl<key extends comparable<key> ,value> implements bst<key ,value> { @override public void put(key key, value value) { // todo auto-generated method stub } @override public value get(key key) { // todo auto-generated method stub return null; } @override public void delete(key key) { // todo auto-generated method stub } @override public iterable<key> keys() { // todo auto-generated method stub return null; } } why extends token cause error in first place? can please explain?
because
public class bstimpl<key extends comparable<key> ,value> implements bst<key extends comparable<key>,value> { ^ type declaration ^ type argument in class declaration, generic type type declaration, can reuse later in class body. in interface implementing, type argument argument. in other words, saying class implements interface type. have give specific type. doesn't make sense key extends comparable... type argument.
the java language spec has more details
the type parameter section follows class name , delimited angle brackets.
and in the section superclasses
if typedeclspecifier followed type arguments, must correct invocation of type declaration denoted typedeclspecifier, , none of type arguments may wildcard type arguments, or compile-time error occurs.
Comments
Post a Comment