Why declare an interface as abstract?
What's point of declaring an interface as abstract? Same thing for an interface method. Is there a point to it?
eg.
public abstract interface Presenter {
public abstract void go(final HasWidgets contai开发者_如何学Cner);
}
Where did you come across the chunk of code you have posted, any old java code base ?
This is what the JLS has to say :
9.1.1.1 abstract Interfaces:
Every interface is implicitly abstract. This modifier is obsolete and should not
be used in new programs.
9.4 Abstract Method Declarations:
For compatibility with older versions of the Java platform, it is permitted but
discouraged, as a matter of style, to redundantly specify the abstract modifier
for methods declared in interfaces.
Interfaces and interface methods are implicitly abstract
even if not declared as so. So there is no need to explicitly specify it.
Makes no difference - interfaces and interface methods are always abstract but you don't have to add the modifier (and interface methods are always public so you don't need the public modifier too).
From the JLS:
9.1.1.1 abstract Interfaces
Every interface is implicitly abstract. This modifier is obsolete and should not be used in new programs.
Typically, you don't declare the interface, or its methods, as abstract. They are implicitly.
The methods are also public, so you can skip that also. :-)
have a look at this post
https://stackoverflow.com/questions/4380796/what-is-public-abstract-interface-in-java/4381308#4381308
interface is %100 abstract class.
the keyword abstract is redundant here
"Why declare an interface as abstract?"-I got the same question and thought that abstract is redundant. But had to rethink when I saw the Map interface in java 1.8.May be this has to be changed in java
// (version 1.8 : 52.0, no super bit)
// Signature: <K:Ljava/lang/Object;V:Ljava/lang/Object;>Ljava/lang/Object;
public abstract interface java.util.Map {
// Method descriptor #1 ()I
public abstract int size();
}
The default behavior of an interface is essentially equivalent to what you have in your example. Defining it as abstract is just redundant.
I think just verboseness, explicitness and consistency with the class syntax and semantics...
You don't have to, but maybe it could help if some reader of your code is distracted or not very versed in Java.
There is no point of declaring interface to be abstract. As the methods in the interface are abstract only.. One more thing abstract class can have both concrete and abstract methods but in the interface there should be only abstract methods.
The abstract modifier for an interface method is always redundant as well as the public modifier.
The abstract modifier on the interface itself may be redundant for a strict technical reason as an interface can never be instantiated using the new operator and the interface will always be abstract if asked via reflection.
However, there can be a semantic reason for declaring an interface abstract (that is also supported by various UML tools): You might want to express that an interface is explicitly declared abstract in the way that a non-abstract class may not implement the interface directly but only via a sub-interface. So e.g. you might consider the interface Node as semantically abstract while the sub-interfaces Folder and File that extend Node are semantically not abstract. You will never have an instance that is only a Node - it will be either a Folder or a File.
Even further there are frameworks that allow "instantiation" of interfaces (technically via dynamic proxies). There some interface (e.g. a predefined base interface) are not allowed to supply as argument. For documentation purpose it can make sense in the source code to use the abstract modifier to express such information.
Question: Can we declare an Interface with “abstract” keyword? Answer: Yes, we can declare an interface with “abstract” keyword. But, there is no need to write like that. All interfaces in java are abstract by default. Same applies to interface methods
Please go throw this link https://javaconceptoftheday.com/java-interview-questions-on-interfaces/
INTERFACE CAN HAVE STATIC METHOD SINCE JAVA8
精彩评论