简体   繁体   中英

java generics subtype in interface

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 Comparable<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 , ie 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>{}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM