Java 8中的抽象类和接口有什么区别? [英] What are the differences between abstract classes and interfaces in Java 8?

查看:262
本文介绍了Java 8中的抽象类和接口有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Java中,抽象类和接口之间曾经有一个微妙但重要的区别:默认实现。抽象类可以拥有它们,接口不可以。 Java 8虽然引入了接口的默认实现,但这意味着它不再是接口和抽象类之间的关键区别。

In Java there used to be a subtle but important difference between abstract classes and interfaces: default implementations. Abstract classes could have them, interfaces could not. Java 8 though introduces default implementations for interfaces, meaning this is no longer the critical difference between an interface and an abstract class.

那是什么?

据我所知,唯一剩下的差异(除了可能是引擎效率之外的东西)是抽象类遵循传统的Java单继承,而接口可以有多重继承(或者多重实施,如果你愿意)。这引出了另一个问题 -

As best as I can tell, the only remaining difference (besides perhaps some under the hood efficiency stuff) is that abstract classes follow traditional Java single-inheritance, whereas interfaces can have multiple-inheritance (or multiple-implementation if you will). This leads me to another question -

新的Java 8接口如何避免钻石问题

How do the new Java 8 interfaces avoid the diamond Problem?

推荐答案

接口不能有与之关联的状态。

Interfaces cannot have state associated with them.

抽象类可以有与之关联的状态。

Abstract classes can have state associated with them.

此外,接口中的默认方法无需实现。所以这样,它不会破坏已经存在的代码,因为当接口确实收到更新时,实现类不需要实现它。

结果你可能得到次优的代码,但是如果你想拥有更优化的代码,那么你的工作就是覆盖默认的实现。

Furthermore, default methods in interfaces need not be implemented. So in this way, it will not break already existing code, as while the interface does receive an update, the implementing class does not need to implement it.
As a result you may get suboptimal code, but if you want to have more optimal code, then your job is to override the default implementation.

最后,如果发生钻石问题,编译器会发出警告, 需要选择要实现的接口。

And lastly, in case a diamond problem occurs, then the compiler will warn you, and you will need to choose which interface you want to implement.

要显示有关钻石问题的更多信息,请考虑以下代码:

To show more about the diamond problem, consider the following code:

interface A {
    void method();
}

interface B extends A {
    @Override
    default void method() {
        System.out.println("B");
    }
}

interface C extends A { 
    @Override
    default void method() {
        System.out.println("C");
    }
}

interface D extends B, C {

}

这里我得到编译器错误接口D扩展B,C ,那:

Here I get the compiler error on interface D extends B, C, that:

接口D继承方法()表单类型B和C的无关默认值

修复是:

interface D extends B, C {
    @Override
    default void method() {
        B.super.method();
    }
}

如果我想继承方法()来自 B

如果 D 是一个

In case I wanted to inherit the method() from B.
The same holds for if D were a class.

更多地展示Java中接口和抽象类之间的区别8,考虑以下团队

To show even more about the difference between interfaces and abstract classes in Java 8, consider the following Team:

interface Player {

}

interface Team {
    void addPlayer(Player player);
}

理论上你可以提供 addPlayer的默认实现这样你就可以将玩家添加到例如玩家列表中。

但等等......?

如何存储玩家列表?

答案是你不能在界面中这样做,即使你有默认的实现。

You can in theory provide a default implementation of addPlayer such that you can add players to for example a list of players.
But wait...?
How do I store the list of players?
The answer is that you cannot do that in an interface, even if you have default implementations available.

这篇关于Java 8中的抽象类和接口有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆