我一直在试图理解在接口涉及到继承时的继承关系。我想知道如果遵循以下方式,子类是如何创建的:
例如,假设我有:
- 一个实现接口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");}
}