开发者

java generics subtype in interface

开发者 https://www.devze.com 2023-03-15 11:05 出处:网络
If I have the following interface and I want to implement it public interface A<E extends Comparable<E>>{

If I have the following interface and I want to implement it

public interface A<E extends Comparable<E>>{
    //code
}

What is the correct syntax for the implementing class declaration? I get an error when I do this

public class B<E extends Comparable<E>> implements A<E extends C开发者_开发问答omparable<E>>{}

Should it just read implements A<E> or just implements A?


Where the Comparable type E is, for example, String, you would want:

public class B implements A<String> { ... }

Where you want to retain the generic type parameter declaration in B, you would have:

public static class B<E extends Comparable<E>> implements A<E> { ... }

Note that the E in A is not related to the E in B, i.e. the following is valid:

public static class B<Foo extends Comparable<Foo>> implements A<Foo> { ... }

(whether you want to distinguish or not in your code I don't know, but it might help in understanding)


public class B<E extends Comparable<E>> implements A<E>{}
0

精彩评论

暂无评论...
验证码 换一张
取 消