Java 8允许在称为default methods的接口中默认实现方法。
我在什么时候使用那种接口默认方法,而不是抽象类(带有抽象方法)之间感到困惑。
那么什么时候应该使用默认方法的接口,什么时候应该使用抽象类(带有抽象方法)?抽象类在这种情况下仍然有用吗?
Java 8允许在称为default methods的接口中默认实现方法。
我在什么时候使用那种接口默认方法,而不是抽象类(带有抽象方法)之间感到困惑。
那么什么时候应该使用默认方法的接口,什么时候应该使用抽象类(带有抽象方法)?抽象类在这种情况下仍然有用吗?
当前回答
抽象类比默认方法实现(如私有状态)要多得多,但从Java 8开始,无论何时您可以选择其中任何一种,都应该选择防御器(也就是私有状态)。默认)方法。
默认方法的限制是它只能通过调用其他接口方法来实现,而不能引用特定实现的状态。所以主要的用例是高级和方便的方法。
这个新特性的好处在于,以前您必须使用抽象类来实现方便的方法,从而将实现者限制为单一继承,现在您可以拥有一个真正干净的设计,只需要接口,并且强制程序员进行最少的实现工作。
向Java 8引入默认方法的最初动机是希望在不破坏任何现有实现的情况下,使用面向lambda的方法扩展集合框架接口。虽然这与公共图书馆的作者更相关,但您可能会发现相同的特性在您的项目中也很有用。您有一个集中的地方可以添加新的便利,而不必依赖于类型层次结构的其余部分。
其他回答
本文将对此进行描述。想想forEach of Collections。
List<?> list = …
list.forEach(…);
The forEach isn’t declared by java.util.List nor the java.util.Collection interface yet. One obvious solution would be to just add the new method to the existing interface and provide the implementation where required in the JDK. However, once published, it is impossible to add methods to an interface without breaking the existing implementation. The benefit that default methods bring is that now it’s possible to add a new default method to the interface and it doesn’t break the implementations.
有一些技术上的差异。与Java 8接口相比,抽象类仍然可以做更多的事情:
抽象类可以有构造函数。 抽象类更加结构化,可以保存状态。
从概念上讲,防御方法的主要目的是在Java 8中引入新特性(如lambda-functions)后实现向后兼容性。
如本文所述,
Java 8中的抽象类与接口
After introducing Default Method, it seems that interfaces and abstract classes are same. However, they are still different concept in Java 8. Abstract class can define constructor. They are more structured and can have a state associated with them. While in contrast, default method can be implemented only in the terms of invoking other interface methods, with no reference to a particular implementation's state. Hence, both use for different purposes and choosing between two really depends on the scenario context.
Remi Forax规则是不要用抽象类进行设计。你用界面来设计你的应用。无论Java的版本是什么,无论语言是什么。它以SOLID原理中的界面分离原理为依据。
稍后您可以使用抽象类来分解代码。现在有了Java 8,你可以直接在界面中完成。这只是个设施,仅此而已。
虽然这是一个老问题,但让我也谈谈我的看法。
abstract class: Inside abstract class we can declare instance variables, which are required to the child class Interface: Inside interface every variables is always public static and final we cannot declare instance variables abstract class: Abstract class can talk about state of object Interface: Interface can never talk about state of object abstract class: Inside Abstract class we can declare constructors Interface: Inside interface we cannot declare constructors as purpose of constructors is to initialize instance variables. So what is the need of constructor there if we cannot have instance variables in interfaces. abstract class: Inside abstract class we can declare instance and static blocks Interface: Interfaces cannot have instance and static blocks. abstract class: Abstract class cannot refer lambda expression Interfaces: Interfaces with single abstract method can refer lambda expression abstract class: Inside abstract class we can override OBJECT CLASS methods Interfaces: We cannot override OBJECT CLASS methods inside interfaces.
最后,我要指出:
Default method concepts/static method concepts in interface came just to save implementation classes but not to provide meaningful useful implementation. Default methods/static methods are kind of dummy implementation, "if you want you can use them or you can override them (in case of default methods) in implementation class" Thus saving us from implementing new methods in implementation classes whenever new methods in interfaces are added. Therefore interfaces can never be equal to abstract classes.