Many junior developers make the mistake of thinking of interfaces, abstract and concrete classes as slight variations of the same thing, and choose one of them purely on technical grounds: Do I need multiple inheritance? Do I need some place to put common methods? Do I need to bother with something other than just a concrete class? This is wrong, and hidden in these questions is the main problem: "I". When you write code for yourself, by yourself, you rarely think of other present or future developers working on or with your code.
接口和抽象类,虽然从技术的角度来看很相似,但它们的含义和目的完全不同。
总结
接口定义了一个契约,由某个实现为您实现。
抽象类提供了您的实现可以重用的默认行为。
以上两点正是我在面试时所寻求的,并且是一个足够紧凑的总结。阅读更多细节。
替代的总结
接口用于定义公共api
抽象类用于内部使用和定义spi
通过例子
To put it differently: A concrete class does the actual work, in a very specific way. For example, an ArrayList uses a contiguous area of memory to store a list of objects in a compact manner which offers fast random access, iteration, and in-place changes, but is terrible at insertions, deletions, and occasionally even additions; meanwhile, a LinkedList uses double-linked nodes to store a list of objects, which instead offers fast iteration, in-place changes, and insertion/deletion/addition, but is terrible at random access. These two types of lists are optimized for different use cases, and it matters a lot how you're going to use them. When you're trying to squeeze performance out of a list that you're heavily interacting with, and when picking the type of list is up to you, you should carefully pick which one you're instantiating.
On the other hand, high level users of a list don't really care how it is actually implemented, and they should be insulated from these details. Let's imagine that Java didn't expose the List interface, but only had a concrete List class that's actually what LinkedList is right now. All Java developers would have tailored their code to fit the implementation details: avoid random access, add a cache to speed up access, or just reimplement ArrayList on their own, although it would be incompatible with all the other code that actually works with List only. That would be terrible... But now imagine that the Java masters actually realize that a linked list is terrible for most actual use cases, and decided to switch over to an array list for their only List class available. This would affect the performance of every Java program in the world, and people wouldn't be happy about it. And the main culprit is that implementation details were available, and the developers assumed that those details are a permanent contract that they can rely on. This is why it's important to hide implementation details, and only define an abstract contract. This is the purpose of an interface: define what kind of input a method accepts, and what kind of output is expected, without exposing all the guts that would tempt programmers to tweak their code to fit the internal details that might change with any future update.
抽象类介于接口和具体类之间。它应该帮助实现共享常见或无聊的代码。例如,AbstractCollection提供了基于大小为0的isEmpty的基本实现,contains作为迭代和比较,addAll作为重复添加,等等。这使得实现将重点放在区分它们的关键部分:如何实际存储和检索数据。
另一个角度:api与spi
接口是代码不同部分之间的低内聚网关。它们允许库的存在和发展,而不会在内部发生变化时影响到每个库的用户。它被称为应用程序编程接口,而不是应用程序编程类。在较小的规模上,它们还允许多个开发人员在大型项目上成功协作,通过良好的文档接口分离不同的模块。
抽象类是在实现接口时使用的高内聚帮助器,假设有某种级别的实现细节。或者,抽象类用于定义服务提供者接口(spi)。
API和SPI之间的区别很微妙,但很重要:对于API,重点在于谁使用它,而对于SPI,重点在于谁实现它。
Adding methods to an API is easy, all existing users of the API will still compile. Adding methods to an SPI is hard, since every service provider (concrete implementation) will have to implement the new methods. If interfaces are used to define an SPI, a provider will have to release a new version whenever the SPI contract changes. If abstract classes are used instead, new methods could either be defined in terms of existing abstract methods, or as empty throw not implemented exception stubs, which will at least allow an older version of a service implementation to still compile and run.
关于Java 8和默认方法的说明
尽管Java 8为接口引入了默认方法,这使得接口和抽象类之间的界限更加模糊,但这并不是为了实现可以重用代码,而是为了更容易地更改既作为API又作为SPI(或者被错误地用于定义SPI而不是抽象类)的接口。
“书本知识”
OP回答中提供的技术细节被认为是“书本知识”,因为这通常是在学校和大多数关于语言的技术书籍中使用的方法:一个东西是什么,而不是如何在实践中使用它,特别是在大规模应用中。
打个比方:假设问题是:
舞会之夜租什么更好,一辆车还是一间酒店房间?
技术上的答案是这样的:
嗯,在车里你可以做得更快,但在酒店房间里你可以做得更舒服。另一方面,酒店房间只在一个地方,而在汽车里你可以在更多的地方这样做,比如,你可以去远景点看风景,或者在汽车电影院,或者很多其他地方,甚至不止一个地方。而且,酒店房间里有淋浴。
这都是真的,但完全忽略了一点,那就是它们是两种完全不同的东西,两者都可以同时用于不同的目的,“做”方面并不是这两种选择中最重要的事情。这个答案缺乏视角,它显示了一种不成熟的思维方式,而正确地呈现了真实的“事实”。