[英]Interface does not see concrete implementation
I have 我有
ISomeInterface ISome接口
public interface ISomeInterface{
public void myMethod();
}
AbstractClass 抽象类
public abstract class AbstractClass implements ISomeInterface{
public void myMethod(){
//...here goes implemetations
}
}
ConcreteClass 具体类
public class ConcreteClass extends AbstractClass {
//...
}
Compiler prints than ConcreteClass
is not abstract and does not override abstract method myMethod
in ISomeInterface
. 比ConcreteClass
编译器打印的不是抽象的,也不覆盖ISomeInterface
抽象方法myMethod
。
The idea is to give implementation to one abstract class and then inherit it in classes that extend it. 这个想法是给一个抽象类提供实现,然后在扩展它的类中继承它。 I think that ConcreteClass
should gets implementation form AbstractClass
since it's extending it. 我认为ConcreteClass
应该从AbstractClass
获得实现,因为它是在对其进行扩展。 Right? 对? What's the matter? 怎么了?
UPDATE 更新
I haven't noticed until now that method
is wrong and it has to be myMethod
. 直到现在我还没有注意到该method
是错误的,它必须是myMethod
。 Anyways, same error. 无论如何,同样的错误。
UPDATE2 更新2
The problem was that in AbstractClass them method had correct name but incorrect signature. 问题在于,在AbstractClass中,它们的方法名称正确,但签名不正确。 After changing it according with interface the problem was solved :) 根据接口更改它后,问题解决了:)
In AbstractClass
you are creating a method myMethod()
, but your interface method method()
is not being implemented in ConcreteClass
. 在AbstractClass
您正在创建方法myMethod()
,但您的接口方法method()
并未在ConcreteClass
中实现。 The names are different. 名称是不同的。
Assuming that your sample code is complete, you need to implement method
from your interface. 假设您的示例代码完整,则需要从您的界面中实现method
。 In AbstractClass
you have called it myMethod
instead. 在AbstractClass
您改为将其命名为myMethod
。
You can use the @Override
annotation on any override methods. 您可以在任何覆盖方法上使用@Override
批注。 This will tell you if you have used the wrong parameter types, method name, or an incompatible return type (parameters and return types don't have to be the same; they can, for example, be covariant ). 这将告诉您是否使用了错误的参数类型,方法名称或不兼容的返回类型(参数和返回类型不必相同;例如,它们可以是协变量 )。
Additionally, in Java, we don't tend to prefix interface names with I
(unlike the convention in C#). 此外,在Java中,我们不倾向于在接口名称前加上I
(与C#中的约定不同)。 Your interface would usually be called SomeInterface
and not ISomeInterface
. 您的界面通常称为SomeInterface
而不是ISomeInterface
。
Also, the public
on methods on interfaces is implicit , so you can leave it out. 同样,接口上方法的public
是隐式的 ,因此您可以省去它。 If you do include it you should probably include the abstract
as well to include all of the implicit modifiers. 如果包含它,则可能还应该包含abstract
,以包含所有隐式修饰符。
An updated code sample would be: 更新的代码示例将是:
public interface SomeInterface{
void method();
}
public abstract class AbstractClass implements SomeInterface {
@Override
public void method(){
//...here goes implemetations
}
}
You are probably overriding the wrong method . 您可能覆盖了错误的方法 。 What happens is that you are probably attempting to override a method in your abstract class but what you are actually doing is to just define a new method with a new name. 发生的情况是您可能正在尝试覆盖抽象类中的方法,但实际上是在使用新名称定义一个新方法。 In the interface, the method is named method
but in your abstract class your method is named myMethod
. 在界面中,该方法名为method
但在抽象类中,该方法名为myMethod
。
So, check this out: 因此,请查看以下内容:
public abstract class AbstractClass implements ISomeInterface{
// Not the same name as in the interface
public void myMethod(){
//...here goes implemetations
}
}
In order to solve it, simply change the method name in the subclass to the correct name. 为了解决它,只需将子类中的方法名称更改为正确的名称。
public abstract class AbstractClass implements ISomeInterface{
// Now you have the correct name and inheritance will
// work as expected
@Override
public void method(){
//...here goes implemetations
}
}
This is the perfect case for explaining the @Override
annotation as well ;) 这也是解释@Override
注释的理想情况;)
When overriding a method, you might want to use the @Override annotation that instructs the compiler that you intend to override a method in the superclass. 覆盖方法时,可能需要使用@Override批注,该注释指示编译器打算覆盖超类中的方法。 If, for some reason, the compiler detects that the method does not exist in one of the superclasses, then it will generate an error. 如果由于某种原因,编译器检测到该方法在超类之一中不存在,则它将生成错误。
When you declare a method with the annotation @Override
the overridden method must match the signature of the interface-method (or superclass method). 当使用注解@Override
声明方法时,被覆盖的方法必须与接口方法(或超类方法)的签名匹配。 Read more about @Override
in the Oracle Docs . 在Oracle文档中了解有关@Override
更多信息。
And, if you are not trying to override the method named method
in your abstract class you simply need to add that method to your concrete class like this: 而且,如果您不打算在抽象类中覆盖名为method
,则只需要将该方法添加到您的具体类中,如下所示:
public class ConcreteClass extends AbstractClass {
// Now we are implementing the correct method from the interface
// If not, there will be a compiler error.
@Override
public void method() {
}
//...
}
On a side note which may be relevant: methods can have the same name but with different argument lists. 附带说明一下:方法可以具有相同的名称,但具有不同的参数列表。 This is known as overloading (or overloaded methods) which you can read more about in this article . 这就是所谓的重载 (或重载方法),您可以在本文中了解更多信息。
Edit: Since the OP is using Java 5 this question may be interesting. 编辑:由于OP使用Java 5, 这个问题可能很有趣。 The @Override
annotation changed between Java 5 and Java 6. In Java 5 it was not allowed to use the @Override
annotation when implementing a method from an interface, it was just allowed when overriding a method from a superclass. 在Java 5和Java 6之间更改了@Override
注释。在Java 5中,从接口实现方法时不允许使用@Override
注释,而从超类覆盖方法时才允许使用@Override
注释。
Your interface defines 您的界面定义
public void method();
but your abstract class has 但是你的抽象类有
public void myMethod(){
//...here goes implemetations
}
Which isn't the same! 哪个不一样! Add the Override annotation to catch that kind of issue at compile time. 添加Override批注以在编译时捕获此类问题。
@Override
public void myMethod(){ // <-- compile error.
//...here goes implemetations
}
From the linked Javadoc, 在链接的Javadoc中,
Indicates that a method declaration is intended to override a method declaration in a supertype. 指示方法声明旨在覆盖超类型中的方法声明。
好吧,我注意到的第一件事是您的方法在接口和抽象类中的名称不同。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.