implements vs extends in generics in Java

前端 未结 3 2085
长发绾君心
长发绾君心 2020-12-29 08:09

Can someone tell me what the differences between the first and second codes are? MaxPQ stands for priority queue, which is a collection of \"Key\" objects that can be compar

相关标签:
3条回答
  • 2020-12-29 08:45

    The difference is pretty straightforward: second code snippet does not compile and never will. With generics you always use extends, for both classes and interfaces. Also super keyword can be used there, but it has different semantics.

    0 讨论(0)
  • 2020-12-29 08:56

    There is no implements in generics. The second code is invalid. You probably confusing with :

    public class MaxPQ implements Comparable<Key> {
       ...
    }
    
    0 讨论(0)
  • 2020-12-29 09:12

    I assume it was decided to use extends for both interfaces and classes, because in the case of generic class declaration it does not make any difference is type argument bound to interface or to class.

    Of course meaning of extends is quite different from its typical usage in class definition. Angelika Langer do have nice text about different meanings of extends in Java: Does "extends" always mean "inheritance"?

    0 讨论(0)
提交回复
热议问题