Should a method that implements an interface method be annotated with @Override
?
是否应该用@Override来注释实现接口方法的方法?
The javadoc of the Override
annotation says:
重写注释的javadoc说:
Indicates that a method declaration is intended to override a method declaration in a superclass. If a method is annotated with this annotation type but does not override a superclass method, compilers are required to generate an error message.
指示方法声明的目的是覆盖超类中的方法声明。如果一个方法是用这个注释类型注释的,但是没有覆盖超类方法,则需要编译器来生成错误消息。
I don't think that an interface is technically a superclass. Or is it?
我不认为接口在技术上是超类。或者是吗?
问题阐述
14 个解决方案
#1
256
You should use @Override whenever possible. It prevents simple mistakes from being made. Example:
您应该尽可能使用@Override。它防止人们犯简单的错误。例子:
class C {
@Override
public boolean equals(SomeClass obj){
// code ...
}
}
This doesn't compile because it doesn't properly override public boolean equals(Object obj)
.
它不能编译,因为它不能正确覆盖公共布尔值(对象obj)。
The same will go for methods that implement an interface (1.6 and above only) or override a Super class's method.
实现接口(1.6或以上)或覆盖超类方法的方法也是如此。
#2
91
I believe that javac behaviour has changed - with 1.5 it prohibited the annotation, with 1.6 it doesn't. The annotation provides an extra compile-time check, so if you're using 1.6 I'd go for it.
我认为javac行为已经发生了变化——1.5禁止注释,1.6禁止注释。注释提供了额外的编译时检查,因此如果您使用1.6版本,我将使用它。
#3
50
You should always annotate methods with @Override
if it's available.
如果方法可用,应该始终使用@Override注释方法。
In JDK 5 this means overriding methods of superclasses, in JDK 6, and 7 it means overriding methods of superclasses, and implementing methods of interfaces. The reason, as mentioned previously, is it allows the compiler to catch errors where you think you are overriding (or implementing) a method, but are actually defining a new method (different signature).
在JDK 5中,这意味着重写超类的方法,在JDK 6和7中,这意味着重写超类的方法,以及实现接口的方法。正如前面提到的,原因是它允许编译器捕获您认为正在覆盖(或实现)一个方法,但实际上正在定义一个新方法(不同的签名)的错误。
The equals(Object)
vs. equals(YourObject)
example is a standard case in point, but the same argument can be made for interface implementations.
equals(Object)与equals(YourObject)示例是一个标准的例子,但是对于接口实现可以使用相同的参数。
I'd imagine the reason it's not mandatory to annotate implementing methods of interfaces is that JDK 5 flagged this as a compile error. If JDK 6 made this annotation mandatory, it would break backwards compatibility.
我认为不强制注释接口实现方法的原因是JDK 5将其标记为编译错误。如果JDK 6强制使用这个注释,它将破坏向后兼容性。
I am not an Eclipse user, but in other IDEs (IntelliJ), the @Override
annotation is only added when implementing interface methods if the project is set as a JDK 6+ project. I would imagine that Eclipse is similar.
我不是Eclipse用户,但是在其他ide (IntelliJ)中,只有在将项目设置为JDK 6+项目时,才会在实现接口方法时添加@Override注释。我认为Eclipse是相似的。
However, I would have preferred to see a different annotation for this usage, maybe an @Implements
annotation.
但是,我更希望看到这个用法的不同注释,可能是@ implementation注释。
#4
14
I would use it at every opportunity. See When do you use Java's @Override annotation and why?
我会利用一切机会。看看什么时候使用Java的@Override注释,为什么?
#5
11
JDK 5.0 does not allow you to use @Override
annotation if you are implementing method declared in interface (its compilation error), but JDK 6.0 allows it. So may be you can configure your project preference according to your requirement.
如果您正在实现接口中声明的方法(它的编译错误),JDK 5.0不允许您使用@Override注释,但是JDK 6.0允许使用它。因此,您可以根据您的需求配置您的项目首选项。
#6
3
Overriding your own methods inherited from your own classes will typically not break on refactorings using an ide. But if you override a method inherited from a library it is recommended to use it. If you dont, you will often get no error on a later library change, but a well hidden bug.
重写从自己的类继承的方法通常不会破坏使用ide进行的重构。但是如果重写从库继承的方法,建议使用它。如果您不这样做,您在以后的库更改中通常不会得到错误,而是会得到一个隐藏得很好的错误。
#7
3
It's not a problem with JDK. In Eclipse Helios, it allows the @Override annotation for implemented interface methods, whichever JDK 5 or 6. As for Eclipse Galileo, the @Override annotation is not allowed, whichever JDK 5 or 6.
这不是JDK的问题。在Eclipse Helios中,它允许为实现的接口方法(无论JDK 5还是6)使用@Override注释。对于Eclipse Galileo,不允许使用@Override注释,无论JDK 5还是6。
#8
2
For me, often times this is the only reason some code requires Java 6 to compile. Not sure if it's worth it.
对我来说,通常这是一些代码需要Java 6编译的唯一原因。不确定是否值得。
#9
1
Eclipse itself will add the @Override
annotation when you tell it to "generate unimplemented methods" during creation of a class that implements an interface.
在创建实现接口的类时,当您告诉它“生成未实现的方法”时,Eclipse本身将添加@Override注释。
#10
1
If a concrete class is not overriding an abstract method, using @Override
for implementation is an open matter since the compiler will invariably warn you of any unimplemented methods. In these cases, an argument can be made that it detracts from readability -- it is more things to read on your code and, to a lesser degree, it is called @Override
and not @Implement
.
如果一个具体的类没有覆盖一个抽象的方法,那么使用@Override实现是开放的,因为编译器总是会警告您任何未实现的方法。在这些情况下,可以提出这样一个论点:它降低了可读性——在代码上读更多的东西,在更小的程度上,它被称为@Override,而不是@ implementation。
#11
1
The problem with including the @Override
is that it makes you think that you forgot to call the super.theOverridenMethod()
method, which is very confusing. This should be crystal-clear. Perhaps Java should offer an @Interface
to be used here. Oh well, yet another half-assed Java peculiarity...
包含@Override的问题是,它会使您认为忘记调用super.theOverridenMethod()方法,这非常令人困惑。这应该是非常清楚的。也许Java应该提供一个在这里使用的@Interface。哦,好吧,又是一个不怎么样的Java特性……
#12
0
In java 6 and later versions, you can use @Override
for a method implementing an interface.
在java 6和以后的版本中,您可以使用@Override来实现接口的方法。
But, I donot think it make sense: override means you hava a method in the super class, and you are implementing it in the sub class.
但是,我不认为它有意义:override意味着在超类中有一个方法,并且在子类中实现它。
If you are implementing an interface, I think we should use @Implement
or something else, but not the @Override
.
如果您正在实现一个接口,我认为我们应该使用@ implementation或其他东西,而不是@Override。
#13
0
By reading the javadoc in java8, you can find the following at the declaration of interface Override:
通过阅读java8中的javadoc,您可以在接口覆盖声明中找到以下内容:
If a method is annotated with this annotation type compilers are required to generate an error message unless at least one of the following conditions hold:
如果用这种注释类型的编译器进行注释,则需要生成一条错误消息,除非至少有以下条件之一:
- The method does override or implement a method declared in a supertype.
- 该方法确实覆盖或实现超类型中声明的方法。
- The method has a signature that is override-equivalent to that of any public method declared in {@linkplain Object}.
- 该方法具有与在{@linkplain Object}中声明的任何公共方法等价的签名。
So, at least in java8, you should use @Override on an implementation of an interface method.
因此,至少在java8中,您应该在接口方法的实现上使用@Override。
#14
0
For the interface, using @Override caused compile error. So, I had to remove it.
对于接口,使用@Override会导致编译错误。所以我必须把它去掉。
Error message went "The method getAllProducts() of type InMemoryProductRepository must override a superclass method
".
错误消息是:“InMemoryProductRepository类型的方法getAllProducts()必须重写超类方法”。
It also read "One quick fix available: Remove @Override annotation.
"
它还显示了“一个可用的快速修复:删除@Override注释”。
It was on Eclipse 4.6.3, JDK 1.8.0_144.
它在Eclipse 4.6.3、JDK 1.8.0_144上。
#1
256
You should use @Override whenever possible. It prevents simple mistakes from being made. Example:
您应该尽可能使用@Override。它防止人们犯简单的错误。例子:
class C {
@Override
public boolean equals(SomeClass obj){
// code ...
}
}
This doesn't compile because it doesn't properly override public boolean equals(Object obj)
.
它不能编译,因为它不能正确覆盖公共布尔值(对象obj)。
The same will go for methods that implement an interface (1.6 and above only) or override a Super class's method.
实现接口(1.6或以上)或覆盖超类方法的方法也是如此。
#2
91
I believe that javac behaviour has changed - with 1.5 it prohibited the annotation, with 1.6 it doesn't. The annotation provides an extra compile-time check, so if you're using 1.6 I'd go for it.
我认为javac行为已经发生了变化——1.5禁止注释,1.6禁止注释。注释提供了额外的编译时检查,因此如果您使用1.6版本,我将使用它。
#3
50
You should always annotate methods with @Override
if it's available.
如果方法可用,应该始终使用@Override注释方法。
In JDK 5 this means overriding methods of superclasses, in JDK 6, and 7 it means overriding methods of superclasses, and implementing methods of interfaces. The reason, as mentioned previously, is it allows the compiler to catch errors where you think you are overriding (or implementing) a method, but are actually defining a new method (different signature).
在JDK 5中,这意味着重写超类的方法,在JDK 6和7中,这意味着重写超类的方法,以及实现接口的方法。正如前面提到的,原因是它允许编译器捕获您认为正在覆盖(或实现)一个方法,但实际上正在定义一个新方法(不同的签名)的错误。
The equals(Object)
vs. equals(YourObject)
example is a standard case in point, but the same argument can be made for interface implementations.
equals(Object)与equals(YourObject)示例是一个标准的例子,但是对于接口实现可以使用相同的参数。
I'd imagine the reason it's not mandatory to annotate implementing methods of interfaces is that JDK 5 flagged this as a compile error. If JDK 6 made this annotation mandatory, it would break backwards compatibility.
我认为不强制注释接口实现方法的原因是JDK 5将其标记为编译错误。如果JDK 6强制使用这个注释,它将破坏向后兼容性。
I am not an Eclipse user, but in other IDEs (IntelliJ), the @Override
annotation is only added when implementing interface methods if the project is set as a JDK 6+ project. I would imagine that Eclipse is similar.
我不是Eclipse用户,但是在其他ide (IntelliJ)中,只有在将项目设置为JDK 6+项目时,才会在实现接口方法时添加@Override注释。我认为Eclipse是相似的。
However, I would have preferred to see a different annotation for this usage, maybe an @Implements
annotation.
但是,我更希望看到这个用法的不同注释,可能是@ implementation注释。
#4
14
I would use it at every opportunity. See When do you use Java's @Override annotation and why?
我会利用一切机会。看看什么时候使用Java的@Override注释,为什么?
#5
11
JDK 5.0 does not allow you to use @Override
annotation if you are implementing method declared in interface (its compilation error), but JDK 6.0 allows it. So may be you can configure your project preference according to your requirement.
如果您正在实现接口中声明的方法(它的编译错误),JDK 5.0不允许您使用@Override注释,但是JDK 6.0允许使用它。因此,您可以根据您的需求配置您的项目首选项。
#6
3
Overriding your own methods inherited from your own classes will typically not break on refactorings using an ide. But if you override a method inherited from a library it is recommended to use it. If you dont, you will often get no error on a later library change, but a well hidden bug.
重写从自己的类继承的方法通常不会破坏使用ide进行的重构。但是如果重写从库继承的方法,建议使用它。如果您不这样做,您在以后的库更改中通常不会得到错误,而是会得到一个隐藏得很好的错误。
#7
3
It's not a problem with JDK. In Eclipse Helios, it allows the @Override annotation for implemented interface methods, whichever JDK 5 or 6. As for Eclipse Galileo, the @Override annotation is not allowed, whichever JDK 5 or 6.
这不是JDK的问题。在Eclipse Helios中,它允许为实现的接口方法(无论JDK 5还是6)使用@Override注释。对于Eclipse Galileo,不允许使用@Override注释,无论JDK 5还是6。
#8
2
For me, often times this is the only reason some code requires Java 6 to compile. Not sure if it's worth it.
对我来说,通常这是一些代码需要Java 6编译的唯一原因。不确定是否值得。
#9
1
Eclipse itself will add the @Override
annotation when you tell it to "generate unimplemented methods" during creation of a class that implements an interface.
在创建实现接口的类时,当您告诉它“生成未实现的方法”时,Eclipse本身将添加@Override注释。
#10
1
If a concrete class is not overriding an abstract method, using @Override
for implementation is an open matter since the compiler will invariably warn you of any unimplemented methods. In these cases, an argument can be made that it detracts from readability -- it is more things to read on your code and, to a lesser degree, it is called @Override
and not @Implement
.
如果一个具体的类没有覆盖一个抽象的方法,那么使用@Override实现是开放的,因为编译器总是会警告您任何未实现的方法。在这些情况下,可以提出这样一个论点:它降低了可读性——在代码上读更多的东西,在更小的程度上,它被称为@Override,而不是@ implementation。
#11
1
The problem with including the @Override
is that it makes you think that you forgot to call the super.theOverridenMethod()
method, which is very confusing. This should be crystal-clear. Perhaps Java should offer an @Interface
to be used here. Oh well, yet another half-assed Java peculiarity...
包含@Override的问题是,它会使您认为忘记调用super.theOverridenMethod()方法,这非常令人困惑。这应该是非常清楚的。也许Java应该提供一个在这里使用的@Interface。哦,好吧,又是一个不怎么样的Java特性……
#12
0
In java 6 and later versions, you can use @Override
for a method implementing an interface.
在java 6和以后的版本中,您可以使用@Override来实现接口的方法。
But, I donot think it make sense: override means you hava a method in the super class, and you are implementing it in the sub class.
但是,我不认为它有意义:override意味着在超类中有一个方法,并且在子类中实现它。
If you are implementing an interface, I think we should use @Implement
or something else, but not the @Override
.
如果您正在实现一个接口,我认为我们应该使用@ implementation或其他东西,而不是@Override。
#13
0
By reading the javadoc in java8, you can find the following at the declaration of interface Override:
通过阅读java8中的javadoc,您可以在接口覆盖声明中找到以下内容:
If a method is annotated with this annotation type compilers are required to generate an error message unless at least one of the following conditions hold:
如果用这种注释类型的编译器进行注释,则需要生成一条错误消息,除非至少有以下条件之一:
- The method does override or implement a method declared in a supertype.
- 该方法确实覆盖或实现超类型中声明的方法。
- The method has a signature that is override-equivalent to that of any public method declared in {@linkplain Object}.
- 该方法具有与在{@linkplain Object}中声明的任何公共方法等价的签名。
So, at least in java8, you should use @Override on an implementation of an interface method.
因此,至少在java8中,您应该在接口方法的实现上使用@Override。
#14
0
For the interface, using @Override caused compile error. So, I had to remove it.
对于接口,使用@Override会导致编译错误。所以我必须把它去掉。
Error message went "The method getAllProducts() of type InMemoryProductRepository must override a superclass method
".
错误消息是:“InMemoryProductRepository类型的方法getAllProducts()必须重写超类方法”。
It also read "One quick fix available: Remove @Override annotation.
"
它还显示了“一个可用的快速修复:删除@Override注释”。
It was on Eclipse 4.6.3, JDK 1.8.0_144.
它在Eclipse 4.6.3、JDK 1.8.0_144上。