Java 为什么将接口声明为抽象接口?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2134200/
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
Why declare an interface as abstract?
提问by Sudhir Jonathan
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 container);
}
采纳答案by sateesh
Where did you come across the chunk of code you have posted, any old java code base ?
This is what the JLShas to say :
你从哪里看到你发布的那段代码,任何旧的 Java 代码库?
这就是 JLS必须说的:
9.1.1.1 abstract Interfaces:
Every interface is implicitly abstract. This modifier is obsolete and should not
be used in new programs.
9.1.1.1 抽象接口:
每个接口都是隐式抽象的。此修饰符已过时,不应在新程序中使用。
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.
9.4 抽象方法声明:
为了与旧版本的 Java 平台兼容,作为一种风格,允许但不鼓励为接口中声明的方法冗余指定抽象修饰符。
回答by KLE
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. :-)
这些方法也是公开的,所以你也可以跳过它。:-)
回答by notnoop
Interfaces and interface methods are implicitly abstract
even if not declared as so. So there is no need to explicitly specify it.
abstract
即使没有声明,接口和接口方法也是隐式的。所以没有必要明确指定它。
回答by Zack Marrapese
The default behavior of an interface is essentially equivalent to what you have in your example. Defining it as abstract is just redundant.
接口的默认行为本质上等同于您在示例中的行为。将其定义为抽象是多余的。
回答by fortran
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.
您不必这样做,但如果您的代码的某些读者分心或不精通 Java,这可能会有所帮助。
回答by Andreas Dolk
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:
从JLS:
9.1.1.1 abstract Interfaces
Every interface is implicitly abstract. This modifier is obsolete and should not be used in new programs.
9.1.1.1 抽象接口
每个接口都是隐式抽象的。此修饰符已过时,不应在新程序中使用。
回答by giri
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.
将接口声明为抽象是没有意义的。由于接口中的方法只是抽象的。另外一件事抽象类可以同时具有具体和抽象方法,但在接口中应该只有抽象方法。
回答by giri
have a look at this post
看看这个帖子
interface is %100 abstract class.
接口是 %100 抽象类。
the keyword abstractis redundant here
关键字摘要在这里是多余的
回答by J?rg
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.
由于严格的技术原因,接口本身的抽象修饰符可能是多余的,因为接口永远不能使用 new 操作符实例化,并且如果通过反射询问,接口将始终是抽象的。
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.
但是,声明接口抽象可能有语义原因(各种 UML 工具也支持):您可能希望以非抽象类可能不实现接口的方式表达接口被显式声明为抽象直接但只能通过子接口。因此,例如,您可能会认为接口 Node 在语义上是抽象的,而扩展 Node 的子接口 Folder 和 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.
甚至还有一些框架允许接口的“实例化”(技术上通过动态代理)。某些接口(例如预定义的基本接口)不允许作为参数提供。出于文档目的,在源代码中使用抽象修饰符来表达此类信息是有意义的。
回答by Vasishta Kuricheti
"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
“为什么将接口声明为抽象的?”-我遇到了同样的问题,并认为抽象是多余的。但是当我在java 1.8中看到Map接口时不得不重新思考。可能这必须在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();
}