我有一个具有三个抽象方法的接口(interface)。
public interface ThreeDemoInter {
public void a();
public void b();
public void c();
}
我有一个具有相同三个方法的父类:
public class ParentThree {
public void a(){
System.out.println("parent a");
}
public void b(){
System.out.println("parent b");
}
public void c(){
System.out.println("parent c");
}
}
最后一个子类有两个扩展父类和实现接口(interface)的方法
public class ChildThree extends ParentThree implements ThreeDemoInter {
public void a(){
System.out.println("child a");
}
public void b(){
System.out.println("child b");
}
my concern is why not getting error of method c()
我知道它扩展了具有此方法的父类,但背后的概念并不完全清楚。 提前致谢。寻找这段代码背后的解释。
最佳答案
my concern is why not getting error of method c()
因为它的父类(ParentThree)已经为子类(ChildThree)所需要的方法c()定义了实现。
规则说任何实现接口(interface)的类都必须实现接口(interface)中声明的所有方法。
当编译代码检查实现依赖时,首先从父类向下检查请求的层次结构,从而解决实现问题。所以,方法c()的实现请求已经被父类满足了。
关于java继承方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37064679/