简体   繁体   English

即使超类实现了相同的接口,在子类中实现接口是否有任何好处

[英]Is there any benefit in implementing a interface in a subclass even though the superclass implements the same interface

When I was seeing the declaration of ArrayList当我看到ArrayList的声明时

class ArrayList<E> extends AbstractList<E>
    implements List<E>, RandomAccess, Cloneable, java.io.Serializable

which implements List interface even though ArrayList 's superclass AbstractList implements the same List interface.它实现List界面,即使ArrayList的超AbstractList实现相同的List界面。

abstract class AbstractList<E> extends AbstractCollection<E> implements List<E>

Similar declarations can be found on HashMap , LinkedHashMap declarations also.类似的声明也可以在HashMapLinkedHashMap声明中找到。

在此处输入图片说明

In the declaration of LinkedHashMap , it implements Map interface only and not the other interfaces implemented by its superclass HashMap .LinkedHashMap的声明中,只实现了Map接口,不实现其超类HashMap实现的其他接口。

So there might be some benefits of having such declarations.因此,拥有此类声明可能会有一些好处。

There are no functional benefits to declaring them again, it does not affect the behavior in any way.再次声明它们没有任何功能上的好处,它不会以任何方式影响行为。

I guess it's only added to make it clearer which interfaces are implemented.我想它只是为了更清楚地说明实现了哪些接口。

This is done for documentation purposes only, to make it immediately clear to the user of the class which interfaces the class implements.这样做仅用于文档目的,以使类的用户立即清楚类实现的接口。

The redundant implements clause makes no difference to the compiler.冗余的implements子句对编译器没有影响。

Yes.是的。 It could've been omitted.本来可以省略的。 But thus it is immediately visible that it is a List.但是因此可以立即看出它是一个列表。 Otherwise an extra click through the code / documentation would be required.否则将需要额外点击代码/文档。 I think that's the reason - clarity.我认为这就是原因——清晰。

And to add what Joeri Hendrickx commented - it is for the purpose of showing that ArrayList implements List.并添加 Joeri Hendrickx 评论的内容 - 这是为了展示 ArrayList 实现 List。 AbstractList in the whole picture is just for convenience and to reduce code duplication between List implementations.整个画面中的 AbstractList 只是为了方便和减少 List 实现之间的代码重复。

Reference: Why does ArrayList have "implements List"?参考: 为什么 ArrayList 有“实现列表”?

Totally unnecessary.完全没有必要。 I wouldn't do it at all.我根本不会这样做。

It's unclear why they did that by then.目前还不清楚他们当时为什么这样做。 But by now apparently it's a mistake, since everybody is surprised by it when they first notice this odd redundancy.但现在显然这是一个错误,因为当他们第一次注意到这种奇怪的冗余时,每个人都会对此感到惊讶。

好吧,这样您在创建AbstractList的子类时必须实现List<E>方法,并且您还可以将ArrayList用作AbstractList

ArrayList<T> and AbstractList<T> implement List<T> for different purposes. ArrayList<T>AbstractList<T> List<T>为不同的目的实现List<T>

  • List<T> establishes what is required for a class to be a list. List<T>确定类成为列表所需的条件。
  • ArrayList<T> implements List<T> as part of defining its own interface . ArrayList<T>实现List<T>作为定义它自己的接口的一部分 This is essential to what ArrayList<T> is.这对于ArrayList<T>是必不可少的。
  • ArrayList<T> extends AbstractList<T> as part of its own implementation . ArrayList<T>扩展AbstractList<T>作为其自身实现的一部分。 This is entirely optional: one could have implemented ArrayList<T> from scratch without inheriting AbstractList<T> , and the class would work in the same way.这完全是可选的:可以从头开始实现ArrayList<T>而不继承AbstractList<T> ,并且该类将以相同的方式工作。
  • AbstractList<T> is intended as a base class for other implementations of List<T> . AbstractList<T>旨在作为List<T>其他实现的基类。 Instead of establishing an interface, it follows an existing one.它不是建立一个接口,而是遵循一个现有的接口。 AbstractList<T> 's implementation of List<T> is not required, everything would compile and run without it just the same. AbstractList<T>List<T>实现不是必需的,没有它,一切都可以编译和运行。 However, inheriting List<T> lets Java compiler spot potential discrepancies between the interface methods and the methods of AbstractList<T> , so it is a very good idea for AbstractList<T> to implement List<T> interface.但是,继承List<T>可以让 Java 编译器发现接口方法和AbstractList<T>方法之间的潜在差异,因此AbstractList<T>实现List<T>接口是一个非常好的主意。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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