Java 8允许在称为default methods的接口中默认实现方法。
我在什么时候使用那种接口默认方法,而不是抽象类(带有抽象方法)之间感到困惑。
那么什么时候应该使用默认方法的接口,什么时候应该使用抽象类(带有抽象方法)?抽象类在这种情况下仍然有用吗?
Java 8允许在称为default methods的接口中默认实现方法。
我在什么时候使用那种接口默认方法,而不是抽象类(带有抽象方法)之间感到困惑。
那么什么时候应该使用默认方法的接口,什么时候应该使用抽象类(带有抽象方法)?抽象类在这种情况下仍然有用吗?
当前回答
如本文所述,
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.
其他回答
关于你对…
那么什么时候应该使用默认方法的接口,什么时候应该使用抽象类?抽象类在这种情况下仍然有用吗?
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接口也不可能实现。
Remi Forax规则是不要用抽象类进行设计。你用界面来设计你的应用。无论Java的版本是什么,无论语言是什么。它以SOLID原理中的界面分离原理为依据。
稍后您可以使用抽象类来分解代码。现在有了Java 8,你可以直接在界面中完成。这只是个设施,仅此而已。
每当我们在抽象类和接口之间做出选择时,我们总是(几乎)选择默认方法(也称为防御器或虚拟扩展)。
Default methods have put an end to classic pattern of interface and a companion class that implements most or all of the methods in that interface. An example is Collection and AbstractCollection. Now we should implement the methods in the interface itself to provide default functionality. The classes which implement the interface has choice to override the methods or inherit the default implementation. Another important use of default methods is interface evolution. Suppose I had a class Ball as: public class Ball implements Collection { ... }
现在在Java 8中引入了一个新特性流。我们可以通过使用添加到接口的stream方法来获取流。如果stream不是默认方法,那么Collection接口的所有实现都将中断,因为它们不会实现这个新方法。向接口添加非默认方法与源不兼容。
但是假设我们不重新编译这个类,而是使用一个包含这个类Ball的旧jar文件。如果没有这个缺失的方法,类将很好地加载,可以创建实例,似乎一切都在正常工作。但是如果程序在Ball实例上调用stream方法,我们将得到AbstractMethodError。因此,使方法默认解决了这两个问题。
Java 9甚至在接口中提供了私有方法,可以用来封装提供默认实现的接口方法中使用的公共代码逻辑。
请首先考虑开/闭原则。接口中的默认方法确实违反了它。这是Java中的一个不好的特性。它助长了糟糕的设计、糟糕的架构和低软件质量。我建议完全避免使用默认方法。
问自己几个问题: 为什么不能把方法放到抽象类中?那么您是否需要多个抽象类呢?然后想想你的类负责什么。您确定要放入单个类中的所有方法都实现了相同的目的吗?可能你会区分几个目的,然后将你的类分成几个类,每个目的有自己的类。
从业务用例上下文中,接口可用于定义特定的业务规则,其中抽象类将定义启动业务的公共结构。
假设某个企业所有者希望与Amazon和Walmart合作,那么这里定义的接口将是WalmartPartner, AmazonPartner将定义特定的业务规则,抽象类BusinessSetup将获得特定区域的业务设置。
// Interfaces
public interface WalmartPartner {
public static boolean signUpForWalmartBusinessAccount(String BusinessId){
System.out.println("Setting up Walmart Business Partner");
return true;
}
public default void getWalmartDeals(){
System.out.println("Default walmart deal executed !");
}
public abstract void setupShopifyForWalmart();
public abstract void setupWalmartProducts();
public interface AmazonPartner {
public static boolean signUpAsAmazonServicePartner(String BusinessId){
System.out.println("Setting up Amazon Business Partner");
return true;
}
public default void paymentPlatformSetup(){
System.out.println(" Amazon default payment platform is setup");
}
public abstract void setupPrimeMemberDealsByRegion();
public abstract void setupPrimeDeals();
}
// Abstract class
public abstract class BusinessSetup {
String businessId ;
public BusinessSetup(String businessId){
this.businessId = businessId;
System.out.println("1. Initial Business setup for BusienssID: "+this.businessId+" is Complete");
}
public final boolean getBusinessRegisteredInRegion(String region){
System.out.println("2. Business got registered in "+region+ "!");
return true;
}
public abstract void setupCustomerPlatform(String customerId);
public abstract void setupVendorPlatform(String vendorId);
}
// Concrete Class
public class WalMartPartnerImpl extends BusinessSetup implements WalmartPartner {
public WalMartPartnerImpl(String businessId) {
super(businessId);
}
@Override
public void setupCustomerPlatform(String customerId) {
}
@Override
public void setupVendorPlatform(String vendorId) {
}
@Override
public void setupShopifyForWalmart() {
}
@Override
public void setupWalmartProducts() {
}
public static void main(String args[]){
WalMartPartnerImpl walMartPartner = new WalMartPartnerImpl("wal8989");
walMartPartner.getBusinessRegisteredInRegion("california");
walMartPartner.getWalmartDeals();
walMartPartner.setupCustomerPlatform("wal8989");
}
}