简体   繁体   English

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

[英]Are all methods of interface abstract?

I see its written in most places-我看到它在大多数地方都写着-

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

But an interface may also contain default methods and static methods and method bodies exists for default methods and static methods.但是接口也可能包含default methodsstatic methods ,并且存在默认方法和静态方法的方法体。

so are all methods of interface abstract?那么接口的所有方法都是抽象的吗?

From Java 8, an interface may also contain default methods and static methods along with abstract methods.从 Java 8 开始,接口还可以包含默认方法和静态方法以及抽象方法。 Method bodies exist for default and static methods.默认方法和静态方法都存在方法体。

One of the biggest design change in Java 8 is with the concept of interfaces. Java 8 中最大的设计变化之一是接口的概念。 Prior to Java 7, we could have only method declarations in the interfaces.在 Java 7 之前,我们只能在接口中进行方法声明。 But from Java 8, we can have default methods and static methods in the interfaces.但是从 Java 8 开始,我们可以在接口中拥有默认方法和静态方法。

Interface Default Method接口默认方法

For creating a default method in the interface, we need to use “default” keyword with the method signature.为了在接口中创建默认方法,我们需要使用带有方法签名的“default”关键字。

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.如果子类中的实现不佳,此功能可帮助我们避免出现不良结果。

For more check out this有关更多信息,请查看

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 all of its methods are abstract.该类及其所有方法都是抽象的。 An abstract class can have implemented methods but the class itself cannot be instantiated (useful for inheritance and following DRY).抽象类可以有实现的方法,但类本身不能被实例化(对继承和遵循 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. Java 和 C# 将类限制为来自单个父类的固有类。 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但是多重继承也有它的问题,即可怕的钻石问题

Have a look at Oracles docs on Abstract methods and classes.查看有关抽象方法和类的 Oracles 文档。

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® Certified Associate Java SE 8 Programmer I Study Guide Exam 1Z0-808,这是错误的。

. . . . . . because prior to Java 8 all interface methods would be assumed to be abstract.因为在 Java 8 之前,所有接口方法都被假定为抽象的。 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.由于 Java 8 现在包含默认方法和静态方法,并且它们从来都不是抽象的,因此您不能假设编译器会将抽象修饰符隐式应用于所有方法。 (page 346 of the book) (本书第346页)

All method in java interfaces are abstract, only if they are explicitly declared static o default they are not abstract. java接口中的所有方法都是抽象的,只有当它们被显式声明为静态或默认时它们才不是抽象的。

Yes, Interfaces can only have abstract methods.是的,接口只能有抽象方法。

In the Java programming language, an interface is a reference type, similar to a class, that can contain only constants, method signatures, default methods, static methods, and nested types.在 Java 编程语言中,接口是一种引用类型,类似于类,只能包含常量、方法签名、默认方法、静态方法和嵌套类型。 Method bodies exist only for default methods and static methods.方法体只存在于默认方法和静态方法中。 Interfaces cannot be instantiated—they can only be implemented by classes or extended by other interfaces.接口不能实例化——它们只能由类实现或由其他接口扩展。

Source: https://docs.oracle.com/javase/tutorial/java/IandI/createinterface.html来源: https ://docs.oracle.com/javase/tutorial/java/IandI/createinterface.html

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.接口必须具有抽象方法才能应用多重继承。

ALL the method in the interface are Abstract and by default the fields in the JAVA are static , public and final and the all members are public接口中的所有方法都是抽象的,默认情况下 JAVA 中的字段是staticpublicfinal并且所有成员都是公共

and we can't make the members of the interface private and protected .而且我们不能将接口的成员设为privateprotected

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM