简体   繁体   中英

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.

so are all methods of interface abstract?

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.

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.

For more check out this

That is correct

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

A perfect explanation by @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).

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

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

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.

. . . 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)

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

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. 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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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