不是。Java接口中的方法可以是抽象的,也可以是具体实现的。
代码演示:
// 定义接口
public interface MyInterface {
void method1();
default void method2() {
System.out.println(getClass().getSimpleName() + ": 接口方法method2()");
}
}
// 实现类
public static class MyClass implements MyInterface {
@Override
public void method1() {
// 实现方法
System.out.println(getClass().getSimpleName() + ": 实现类方法method1()");
}
}
public static void main(String[] args) {
MyClass myClass = new MyClass();
myClass.method1();
myClass.method2();
}
执行结果:
MyClass: 实现类方法method1()
MyClass: 接口方法method2()
Process finished with exit code 0
在这种情况下,MyInterface接口中的方法method1是抽象方法,必须在实现类MyClass中实现。但是,MyInterface接口中的方法method2是具体实现方法,可以直接在接口中定义。
总结一下,Java接口中的方法可以是抽象的,也可以是具体实现的。抽象方法必须在实现类中实现。具体实现方法可以直接在接口中定义,也可以在实现类中定义。

被折叠的 条评论
为什么被折叠?



