抽象方法未指定主体 - 试图实现超级接口方法的接口在 JDK 8 中给出编译时错误
Abstract methods do not specify a body - Interface trying to implement super interface method giving compile time error in JDK 8
我目前正在尝试学习 JDK 8
功能以及我们可以在接口内执行方法的新功能。像这样
interface SuperInt {
public static void method1() { // completely qualified
System.out.println("method1");
}
public default void method2() { // completely qualified
System.out.println("method2");
}
public void method3(); // completely qualified
}
但是当我尝试扩展此接口并尝试在子接口中实现它时出现编译时错误。
Abstract methods do not specify a body
interface SubInt extends SuperInt {
public void method3() { // compile time error
}
}
如果在接口中保留实现的方法是可以的,那么为什么当我们试图在其子接口中实现超接口的抽象方法时会报错?
But when I tried to extend this interface and tried to implement it in
a sub interface is giving a compile time error.
您没有尝试实现它,而是定义了一个新的抽象方法。
public void method3() { // compile time error
}
如果您想提供实现,请在方法声明前加上 default
关键字:
public default void method3() { // compile time error
...
}
您不能在 interface
中实现抽象方法,并且 SubInt
仍然是 interface
而不是 class
作为
interface SubInt extends SuperInt
正在尝试扩展接口并且没有实现它。要实现它,您应该使用
public class SuperIntImpl implements SuperInt {
@Override
public void method3() {
}
}
另一方面,method2
是一个 default
方法,这就是它编译实现的原因。
与 SubInt
和 SuperInt
的示例相关,在 SubInt
中使用默认覆盖实现,希望这个示例能澄清一些事情:
public interface SuperInt {
void method3();
void method4();
}
public interface SubInt extends SuperInt {
@Override
default void method3() {
System.out.println("Inside SubInt");
}
}
虽然 SubInt
的实施现在可以选择覆盖或不覆盖 method3
,必须 仍然实施 method4
作为
public class SubIntImpl implements SubInt {
@Override
public void method4() {
}
// can reuse the implementation of the 'method3'
}
并且对于 SuperInt
的任何实现,仍然必须有自己的 method3
和 method4
实现
public class SuperIntImpl implements SuperInt {
@Override
public void method3() {
// must have my own implementation
}
@Override
public void method4() {
// must have my own implementation
}
}
我目前正在尝试学习 JDK 8
功能以及我们可以在接口内执行方法的新功能。像这样
interface SuperInt {
public static void method1() { // completely qualified
System.out.println("method1");
}
public default void method2() { // completely qualified
System.out.println("method2");
}
public void method3(); // completely qualified
}
但是当我尝试扩展此接口并尝试在子接口中实现它时出现编译时错误。
Abstract methods do not specify a body
interface SubInt extends SuperInt {
public void method3() { // compile time error
}
}
如果在接口中保留实现的方法是可以的,那么为什么当我们试图在其子接口中实现超接口的抽象方法时会报错?
But when I tried to extend this interface and tried to implement it in a sub interface is giving a compile time error.
您没有尝试实现它,而是定义了一个新的抽象方法。
public void method3() { // compile time error
}
如果您想提供实现,请在方法声明前加上 default
关键字:
public default void method3() { // compile time error
...
}
您不能在 interface
中实现抽象方法,并且 SubInt
仍然是 interface
而不是 class
作为
interface SubInt extends SuperInt
正在尝试扩展接口并且没有实现它。要实现它,您应该使用
public class SuperIntImpl implements SuperInt {
@Override
public void method3() {
}
}
另一方面,method2
是一个 default
方法,这就是它编译实现的原因。
与 SubInt
和 SuperInt
的示例相关,在 SubInt
中使用默认覆盖实现,希望这个示例能澄清一些事情:
public interface SuperInt {
void method3();
void method4();
}
public interface SubInt extends SuperInt {
@Override
default void method3() {
System.out.println("Inside SubInt");
}
}
虽然 SubInt
的实施现在可以选择覆盖或不覆盖 method3
,必须 仍然实施 method4
作为
public class SubIntImpl implements SubInt {
@Override
public void method4() {
}
// can reuse the implementation of the 'method3'
}
并且对于 SuperInt
的任何实现,仍然必须有自己的 method3
和 method4
public class SuperIntImpl implements SuperInt {
@Override
public void method3() {
// must have my own implementation
}
@Override
public void method4() {
// must have my own implementation
}
}