Java 8允许在称为default methods的接口中默认实现方法。

我在什么时候使用那种接口默认方法,而不是抽象类(带有抽象方法)之间感到困惑。

那么什么时候应该使用默认方法的接口,什么时候应该使用抽象类(带有抽象方法)?抽象类在这种情况下仍然有用吗?


当前回答

虽然这是一个老问题,但让我也谈谈我的看法。

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.

其他回答

关于你对…

那么什么时候应该使用默认方法的接口,什么时候应该使用抽象类?抽象类在这种情况下仍然有用吗?

Java文档提供了完美的答案。

抽象类与接口的比较:

Abstract classes are similar to interfaces. You cannot instantiate them, and they may contain a mix of methods declared with or without an implementation. However, with abstract classes, you can declare fields that are not static and final, and define public, protected, and private concrete methods. With interfaces, all fields are automatically public, static, and final, and all methods that you declare or define (as default methods) are public. In addition, you can extend only one class, whether or not it is abstract, whereas you can implement any number of interfaces.

它们各自的用例已经在下面的SE帖子中解释了:

接口和抽象类的区别是什么?

抽象类在这种情况下仍然有用吗?

是的。它们仍然有用。它们可以包含非静态的、非最终的方法和属性(受保护,除了公共之外还有私有),这即使是Java-8接口也不可能实现。

本文将对此进行描述。想想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接口中的默认方法支持接口演进。

对于现有的接口,如果希望在不破坏接口旧版本的二进制兼容性的情况下向其添加方法,有两个选择:添加默认方法或静态方法。实际上,添加到接口中的任何抽象方法都必须由实现该接口的类或接口来实现。

静态方法对于类来说是唯一的。默认方法对于类的实例是唯一的。

如果向现有接口添加默认方法,实现该接口的类和接口不需要实现它。他们可以

实现默认方法,它将覆盖已实现接口中的实现。 重新声明方法(没有实现),使其抽象。 什么都不做(然后简单地继承实现接口的默认方法)。

更多关于这个话题的信息请点击这里。

虽然这是一个老问题,但让我也谈谈我的看法。

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.

这两个是完全不同的:

默认方法是在不改变现有类状态的情况下向其添加外部功能。

抽象类是一种普通的继承类型,它们是用于扩展的普通类。