私はインターフェースが関係しているときの継承を理解しようとしてきました。サブクラスが次の場合にどのように作成されるかを知りたいです。
たとえば、私が持っているとしましょう:
- インターフェイスIを実装するスーパークラス
- スーパークラスAを拡張するいくつかのサブクラス
私の質問
Aを拡張するすべてのサブクラスでインターフェースメソッド「qおよびr」の実装を提供する必要がありますか?
サブクラスでインターフェイスの実装を提供しない場合、そのサブクラスを抽象クラスにする必要がありますか?
サブクラスのいずれかがIを実装することは可能ですか?たとえば、クラスCはAを拡張し、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");}
}