Java 8允许在称为default methods的接口中默认实现方法。
我在什么时候使用那种接口默认方法,而不是抽象类(带有抽象方法)之间感到困惑。
那么什么时候应该使用默认方法的接口,什么时候应该使用抽象类(带有抽象方法)?抽象类在这种情况下仍然有用吗?
Java 8允许在称为default methods的接口中默认实现方法。
我在什么时候使用那种接口默认方法,而不是抽象类(带有抽象方法)之间感到困惑。
那么什么时候应该使用默认方法的接口,什么时候应该使用抽象类(带有抽象方法)?抽象类在这种情况下仍然有用吗?
当前回答
正如在其他回答中提到的,添加实现到接口的能力是为了在Collections框架中提供向后兼容性。我认为,提供向后兼容性可能是向接口添加实现的唯一好的理由。
否则,如果您将实现添加到接口,那么您就违反了最初添加接口的基本规律。Java是一种单一继承语言,不像c++允许多重继承。接口提供了支持多重继承的语言所具有的类型优势,而不会引入多重继承所带来的问题。
更具体地说,Java只允许实现的单一继承,但它允许接口的多重继承。例如,以下是有效的Java代码:
class MyObject extends String implements Runnable, Comparable { ... }
MyObject只继承了一个实现,但它继承了三个契约。
Java传递了实现的多重继承,因为实现的多重继承带来了许多棘手的问题,这些问题超出了本文的讨论范围。添加接口是为了允许合约的多重继承(又名接口),而不存在实现的多重继承问题。
为了支持我的观点,这里引用了Ken Arnold和James Gosling在《Java编程语言》(第4版)一书中的一句话:
Single inheritance precludes some useful and correct designs. The problems of multiple inheritance arise from multiple inheritance of implementation, but in many cases multiple inheritance is used to inherit a number of abstract contracts and perhaps one concrete implementation. Providing a means to inherit an abstract contract without inheriting an implementation allows the typing benefits of multiple inheritance without the problems of multiple implementation inheritance. The inheritance of an abstract contract is termed interface inheritance. The Java programming language supports interface inheritance by allowing you to declare an interface type
其他回答
本文将对此进行描述。想想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 Interface中的默认方法将更多地用于提供函数的虚拟实现,从而使该接口的任何实现类不必声明所有抽象方法,即使它们只想处理一个抽象方法。 因此,接口中的默认方法在某种程度上更像是适配器类概念的替代品。
然而,抽象类中的方法应该给出一个有意义的实现,只有在需要覆盖一个通用功能时,任何子类才应该覆盖它。
请首先考虑开/闭原则。接口中的默认方法确实违反了它。这是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.
Java接口中的默认方法支持接口演进。
对于现有的接口,如果希望在不破坏接口旧版本的二进制兼容性的情况下向其添加方法,有两个选择:添加默认方法或静态方法。实际上,添加到接口中的任何抽象方法都必须由实现该接口的类或接口来实现。
静态方法对于类来说是唯一的。默认方法对于类的实例是唯一的。
如果向现有接口添加默认方法,实现该接口的类和接口不需要实现它。他们可以
实现默认方法,它将覆盖已实现接口中的实现。 重新声明方法(没有实现),使其抽象。 什么都不做(然后简单地继承实现接口的默认方法)。
更多关于这个话题的信息请点击这里。