2016-05-06 6 views
1

私は3つの抽象メソッドを持つ1つのインターフェースを持っています。java継承メソッド

public interface ThreeDemoInter { 
    public void a(); 
    public void b(); 
    public void c(); 
} 

Iが同じ3つの方法を有するつの親クラスを有する:

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"); 
    } 
} 

と最後の子クラスの親クラスを拡張する2つのメソッドを有するインターフェースを

public class ChildThree extends ParentThree implements ThreeDemoInter { 

    public void a(){ 
     System.out.println("child a"); 

    } 
    public void b(){ 
     System.out.println("child b"); 
    } 

}

を実装します

私の懸念はwhです

この方法を持つ親クラスを拡張しているが、その背後にある概念は完全にはわかりません。 ありがとうございます。このコードの背後にある説明を探しています。

+0

ChildThreeクラスには既にメソッドc()があるためです。 –

+0

マインドでは、 '@Override public void c(){super.c();であれば不合理**です。 } 'が必要になりました。 –

答えて

1

その親クラス(ParentThree)がすでに実装が子クラス(ChildThree)が必要とする方法c()のために定義されているので、私の懸念はなぜ方法c()

のエラーを取得されていません。

ルールでは、インターフェイスを実装しているクラスであれば、インターフェイスに宣言されているすべてのメソッドを実装する必要があります。

実装の依存関係をチェックするようにコードをコンパイルすると、まず要求が親クラスから階層までチェックされ、実装の問題が解決されます。したがって、メソッドc()の実装の要求はすでに親クラスによって満たされています。

0

これは、ChildThreeクラスが3つのメソッドをすべて継承したParentThreeクラスを継承しているためです。このクラスを継承してもこれらのメソッドを継承する必要がある場合は、これらのメソッドを再度定義する必要はありません。

1

私たちが知っているように、サブクラスはすべての親クラスメソッドにアクセスできます。 メソッドをカスタマイズする必要がある場合にのみオーバーライドします。なぜそれがメソッドc()を実装する必要がないのかをaccess.thatsにすべての3つのメソッドがあることを意味します。
希望、あなたはポイントを持っています。

+0

申し訳ありません、 – Roushan45

1

あなたのParentThreeではすでにこれらのメソッドが実装されています。 継承ごとに、すべてのメソッドに子クラスがアクセス可能です。 ChildThreeのすべての同じメソッドはメソッドを実装していません。しかし、メソッドをオーバーライドしています。 あなたのコードのようにChildThree上書きする方法は、あなたのChildThreeあなたがThreeDemoInterを実装していますが期待していたとして、なぜあなたがエラーを取得されていませんされてParentThreeのメソッドをオーバーライドしていないで、だから、

public void a(){..} 
public void b(){..} 

です。

1

javaには、どのクラスもインタフェースを実装するコアルールがあります。そのクラスは抽象クラスでない限り、すべてのメソッドも実装する必要があります。

あなたの場合、親クラスであるParentThreeChildThreeという2つのクラスがあります。

ChildThreeThreeDemoInterインターフェイスを実装しているため、すべてのメソッドを実装し、ParentThreeも拡張しています。

ChildThreeはParentThreeの子クラスはとてもParenthreeのすべてのメソッドがparentThreeクラスとchildThreeに実装されている、我々はすべてのメソッドを持っているchildThree()、B()およびc()で今ChildThreeクラスにアクセスすることがありますあなたはクラスローダーや反射によってを使用して、ChildThreeクラスのメソッドを下にリストする場合、クラスがちょうど(メソッドをオーバーライド)とb()

+0

良い書かれていません!理解しやすいビットの快適さ – Roushan45

0

され、それは三つの方法abcを下に一覧表示されます。それはParentThreeクラスを拡張として、それ自身のパブリックメソッド

インタフェースは、それを実装したクラスには三つの方法abcを持つべきであることを定義して、これらのメソッドが用意されています。

インターフェイス実装は、実装クラス上にこれらのメソッドが存在するかどうかをチェックします。 ChildThreeはそのテストに合格し、したがって有効な実装です。

0

は多くの素晴らしい答えのように見えますが、今ではそのコードについて何かを結論づけることもあります。

if a sub class extended the base then method got inherited to sub class 

また、ここではいくつかの行について、javadocのサブクラスについても読んでいます。この場合のように

What You Can Do in a Subclass 

A subclass inherits all of the public and protected members of its parent, no matter what package the subclass is in. If the subclass is in the same package as its parent, it also inherits the package-private members of the parent. You can use the inherited members as is, replace them, hide them, or supplement them with new members: 

The inherited fields can be used directly, just like any other fields. 
You can declare a field in the subclass with the same name as the one in the superclass, thus hiding it (not recommended). 
You can declare new fields in the subclass that are not in the superclass. 
The inherited methods can be used directly as they are. 
You can write a new instance method in the subclass that has the same signature as the one in the superclass, thus overriding it. 
You can write a new static method in the subclass that has the same signature as the one in the superclass, thus hiding it. 
You can declare new methods in the subclass that are not in the superclass. 
You can write a subclass constructor that invokes the constructor of the superclass, either implicitly or by using the keyword super. 

、子クラスChildThreeは、親クラスParentThree &のようなコード

文献の下の子クラス内の任意のメソッドを定義することが必須ではなく、インターフェースThreeDemoInter、 を実装して延びている場合: - java inheritance