What is the benefit of extending a generic by specifying the new type as actual type of generic

前端 未结 5 714
逝去的感伤
逝去的感伤 2021-01-05 14:27

I saw this pattern somewhere:

class A extends B {

}

This structure is a little unusual to extend a generic by specifying the new

5条回答
  •  时光取名叫无心
    2021-01-05 14:42

    You might do something like this when dealing with recursive data structures. For example, nodes in a graph or a tree could be defined as a collection of other nodes:

    class Node extends AbstractList {
        ...
    }
    

    Equally you might see something like this if the abstract/generic type is meant for comparing objects of a similar type, such as is the case with java.lang.Comparable:

    class MyObject implements Comparable {
        public int compareTo(MyObject other) { ... }
    }
    

提交回复
热议问题