抽象类与接口的使用在Java标准库中(从JDK 8开始)是否有示例?

huangapple go评论146阅读模式
英文:

Are there any examples of abstract class vs interface use in the java standard libraries from jdk-8 on forward?

问题

例如,在Java教程(https://docs.oracle.com/javase/tutorial/java/IandI/abstract.html)中,AbstractMap类被用作抽象类的示例。自从jdk8以来,接口方法现在可以是默认的或静态的,除了它已经存在并且以前是有意义的这个事实之外,AbstractMap不能成为一个接口,还有其他原因吗?据我了解,jdk8之后的剩余差异包括:

  1. 抽象类可以用于抽象状态(不限于静态final字段/可以有构造函数),而接口则不能。
  2. 可以实现多个接口(由相关或无关的类实现),而只能扩展一个类。
  3. 接口的新增功能是为了保持向后兼容性。

看起来,如果从头开始,接口在大多数情况下将提供基本相同的功能,决定使用哪个取决于点1和点2,而不管点3是否是扩展接口功能的原因。

我有什么遗漏吗?是否有任何在jdk8或之后制作的标准库类的示例,可以体现这些差异?我正在学习中,如果我有忽略的地方,请原谅我。

英文:

For example, in the java tutorials (https://docs.oracle.com/javase/tutorial/java/IandI/abstract.html), the AbstractMap class is used as an example for an abstract class. Since jdk8, where interface methods can now be default or static, is there any reason (other than the fact that it's already there and previously made sense) that AbstractMap couldn't have been an interface? As far as I understand, the remaining differences post jdk8 are:

  1. An abstract class can be used to abstract state (not constrained to static final fields/ and have can have constructors) where an interface can not.
  2. Multiple interfaces can be implemented (by related or unrelated classes) while only one class can be extended.
  3. The added functionality to interface was to preserve backwards compatibility.

It looks like if done from scratch, an interface would have provided largely the same functionality in most cases, and deciding on which to use boils down to points 1 and 2 regardless of 3 being the reason for the expanded interface functionality.

Is there anything I'm missing? Are there any examples of standard library classes made at or post jdk8 that exemplify these differences? I'm learning so please forgive me if I've overlooked something.

答案1

得分: 3

列表中缺失的一个要点是protected方法。接口只能拥有公共方法。因此,如果你有一个模板方法模式,你希望子类实现某些方法,但不将其公开为公共API,你不能在接口中实现这一点。

一个例子是AbstractList#removeRange(int, int)。还有一个例子是列表中的第一个要点(拥有一个受保护的字段)。

至于为什么AbstractMap在新API中不是一个接口,我不能代表JDK开发人员发言。一个原因是java.util集合共享相似的设计,改变其中一个而不改变其他集合会破坏一致性。还涉及到一些向后兼容性。即使从个人角度来看,我可能不会过于依赖于某个类是否是接口而不是类的事实。

英文:

One point missing from the list is protected methods. An interface can only have public ones. So, if you have a template method pattern, where you want the subclasses to implements something, but not expose it as a public API, you can't do that with an interface.

An example could be AbstractList#removeRange(int, int). Also an example of no.1 from your list (has a protected field).

Now, to why particularily the AbstractMap is not an interface in the new APIs, I can't speak for JDK developers. One thing is the java.util collections share a similar design and changing one but not the others would break consistency. Some backwards compatibility too. Even if personally I wouldn't probably depend on the fact that something is a class rather than an interface.

huangapple
  • 本文由 发表于 2020年5月31日 01:10:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/62106003.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定