java 接口的所有方法都是抽象的吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/34973731/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 23:36:12  来源:igfitidea点击:

Are all methods of interface abstract?

javaandroidinterfaceabstract-class

提问by Android Developer

I see its written in most places-

我看到它写在大多数地方-

"All of the methods in an interface are abstract."

But an interface may also contain default methodsand static methodsand method bodies exists for default methods and static methods.

但是接口也可能包含default methods和,static methods并且存在用于默认方法和静态方法的方法体。

so are all methods of interface abstract?

那么接口的所有方法都是抽象的吗?

回答by TejjD

That is correct

那是对的

All of its methods are abstract, irregardless of its access modifiers.

它的所有方法都是抽象的,无论其访问修饰符如何。

A perfect explanation by @coder:

@coder的完美解释:

An interface is like a "purely" abstract class. The class and allof its methods are abstract. An abstract class can have implemented methods but the class itself cannot be instantiated (useful for inheritance and following DRY).

For an interface, since there isn't any implementation at all they are useful for their purpose: a contract. If you implement the Interface then you must implement the methods in the interface.

So the difference is an abstract class can have implemented methods whereas a interface cannot.

The reason they are separate is so a class can implement several interfaces. Java and C# restrict a class to inherent from a single parent class. Some languages allow you to inherit from multiple classes and you could accomplish the work of an interface via a "purely" abstract class. But multiple inheritance has its problems, namely the dreaded Diamond Problem

接口就像一个“纯粹的”抽象类。该类及其所有方法都是抽象的。抽象类可以实现方法,但类本身不能实例化(对于继承和遵循 DRY 很有用)。

对于接口,由于根本没有任何实现,因此它们对于其目的很有用:合同。如果你实现了接口,那么你必须实现接口中的方法。

所以区别在于抽象类可以实现方法,而接口不能。

它们分开的原因是一个类可以实现多个接口。Java 和 C# 将类限制为来自单个父类的固有类。某些语言允许您从多个类继承,并且您可以通过“纯”抽象类来完成接口的工作。但是多重继承也有它的问题,即可怕的钻石问题

Have a look at Oracles docs on Abstract methods and classes.

查看有关抽象方法和类的Oracle 文档

回答by Android Developer

From Java 8, an interface may also contain default methods and static methods along with abstract methods . Method bodies exist for default and static methods.

从 Java 8 开始,接口还可能包含默认方法和静态方法以及抽象方法。默认方法和静态方法都存在方法体。

One of the biggest design change in Java 8 is with the concept of interfaces. Prior to Java 7, we could have only method declarations in the interfaces. But from Java 8, we can have default methods and static methods in the interfaces.

Interface Default Method

For creating a default method in the interface, we need to use “default” keyword with the method signature.

Interface static methods

Static methods are similar to default methods except that we can't override them in the implementation classes. This feature helps us in avoiding undesired results incase of poor implementation in child classes.

Java 8 中最大的设计变化之一是接口的概念。在 Java 7 之前,我们只能在接口中声明方法。但是从 Java 8 开始,我们可以在接口中使用默认方法和静态方法。

接口默认方法

为了在接口中创建默认方法,我们需要在方法签名中使用“default”关键字。

接口静态方法

静态方法类似于默认方法,只是我们不能在实现类中覆盖它们。此功能可帮助我们避免在子类中执行不佳的情况下产生不希望的结果。

For more check out this

有关更多信息,请查看

回答by Android Developer

This is false as per Jeanne Boyarsky & Scott Selikoff's book, OCA: Oracle? Certified Associate Java SE 8 Programmer I Study Guide Exam 1Z0-808.

根据 Jeanne Boyarsky 和 ​​Scott Selikoff 的书 OCA:Oracle?认证助理 Java SE 8 程序员 I 学习指南考试 1Z0-808。

. . . because prior to Java 8 all interface methods would be assumed to be abstract. Since Java 8 now includes default and static methods and they are never abstract, you cannot assume the abstract modifier will be implicitly applied to all methods by the compiler. (page 346 of the book)

. . . 因为在 Java 8 之前,所有接口方法都被假定为抽象的。由于 Java 8 现在包含默认方法和静态方法并且它们从不抽象,因此您不能假设抽象修饰符将被编译器隐式应用于所有方法。(本书第 346 页)

回答by resla95

All method in java interfaces are abstract, only if they are explicitly declared static o default they are not abstract.

java 接口中的所有方法都是抽象的,只有当它们显式声明为 static o 默认它们不是抽象的。

回答by Krishnan Mishra

All methods in an interface are abstract. This statement is True. It is mandatory for an interface to have abstract methods only to apply multiple inheritance.

接口中的所有方法都是抽象的。这个说法是正确的。接口必须具有抽象方法才能应用多重继承。

回答by Sardar Abdul Wahab CH

ALL the method in the interfaceare Abstractand by default the fields in the JAVA are static, publicand finaland the all members are public

接口中的所有方法都是抽象的,默认情况下,JAVA 中的字段是静态的公共的最终的,所有成员都是公共的。

and we can't make the members of the interface privateand protected.

并且我们不能将接口的成员设置为privateprotected