当涉及接口时,我一直在尝试理解继承。我想知道如果它们遵循以下子类是如何创建的:
例如,假设我有:
- 实现接口 I 的超类
- 和几个扩展超类 A 的子类
我的问题
我是否必须在所有扩展 A 的子类中提供接口方法“q 和 r”的实现?
如果我不在子类中提供接口的实现,我是否必须使该子类成为抽象类?
任何子类都可以实现 I 吗?例如 C 类扩展 A 实现 I,这可能吗?即使它已经扩展了实现 I 的超类?
假设我不提供接口 I 中方法 r 的实现,那么我将不得不创建超类 A 和抽象类!那是对的吗?
我的示例代码:
//superclass
public class A implements I{
x(){System.out.println("superclass x");}
y(){System.out.println("superclass y");}
q(){System.out.println("interface method q");}
r(){System.out.println("interface method r");}
}
//Interface
public Interface I{
public void q();
public void r();
}
//subclass 1
public class B extends A{
//will i have to implement the method q and r?
x(){System.out.println("called method x in B");}
y(){System.out.println("called method y in B");}
}
//subclass 2
public class C extends A{
//will i have to implement the method q and r?
x(){System.out.println("called method x in C");}
y(){System.out.println("called method y in C");}
}