一个实现的类是否可以在其父接口中声明未声明的方法? [英] Can an implemented class have methods NOT declared in its parent interface?
问题描述
我在Eclipse中尝试过此操作,它显示了编译错误.但是,在在线IDE 编译器中尝试相同的操作不会显示任何错误.这就是为什么造成困惑的原因.
I tried this in Eclipse and it shows a compile error. However trying the same in the online IDE Compilr showed no errors. That's why the confusion.
interface Iclass{
void print();
void hey();
}
class sdlfkajl implements Iclass {
public void print(){
System.out.println("Impl class");
}
public void hey(){
System.out.println("Hey!");
}
public void extra(){
System.out.println("Should I be here?");
}
}
显示的错误是此类不能具有未在接口中声明的方法.建议工具提示说我也应该在接口中声明此方法.
The error shown is that this class cannot have methods not declared in the interface. Suggestion tooltip said I should declare this method in the interface too.
推荐答案
您的代码没有错误.
您可能会误将错误警告-可以将IntelliJ设置为将上面的代码标记为警告.
You might be mistaking a warning for an error - IntelliJ can be set to flag the code above as a warning.
您可能还设置了警告,使编译失败.
You might also have a setting for warnings to fail the compile.
所以请检查您的IDE设置!
So check your IDE settings!
这篇关于一个实现的类是否可以在其父接口中声明未声明的方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!