2012-01-27 21 views
6

からメソッド継承されたメソッドの可視性を減らすことはできません:は、私はこのコンパイラエラーを得た親

You cannot reduce the visibility of a inherited method.

私は、次のここでのコード

class Parent {  
    public void func() { 
     System.out.println("in Parent"); 
    } 
} 

public class TestClass extends Parent {  
    public static void main(String args[]) { 
     parent obj=new TestClass(); 
     obj.addTest(); 
    } 

    private void func() { 
     System.out.println("in child");   
    } 
} 

親クラスを持っている公共およびによって上書きされfunc()方法がありますサブクラスTestClassはプライベートです。コンパイラは可視性を減らすことができないというエラーを投げます。技術的に言うと、タイプTestClassのオブジェクトを作成してタイプ親オブジェクトに割り当てるときは、func()メソッドがオーバーライドされているので、TestClassのfunc()は常に呼び出されます。このエラーの原因は何ですか?誰かが私をはっきりと説明できますか?

答えて

20

サブクラスはvoid func()メソッドの可視性がprivateですが、スーパークラスの可視性はpublicです。

あなたのコードがコンパイルさせた場合は、これをしなかった場合、それは実行時に爆発するでしょう:

parent p = new TestClass(); 
p.func(); // boom - func is public in parent, but TestClass's impl is private, so no access would be allowed 

これを "修正" するには、サブクラスのfunc方法publicます

public class TestClass extends parent { 
    ... 
    public void func() { // give it public visibility 
     System.out.println("in child");   
    } 
} 


を 標準の命名規則を使用してください。この場合には「クラスは大文字で始めるべきです」 - すなわちParentないparent

+0

ありがとうBohemain。私は理由がある。命名基準を指摘してくれてありがとう。 – Mojoy

+0

こんにちは..これは私がスーパークラスで公開されているメソッドの子クラスの修飾子を保護していない理由を考えることができますか?プロテクトされた修飾子はパッケージやパッケージのサブタイプ内でアクセスできるので!! – Mojoy

+0

'protected'メソッドは他の任意のクラスからは見えませんが、superの' public'メソッドは同じです。 *可視性が低下するとエラーになります。 – Bohemian

11

section 8.4.8.3 of the Java Language specificationから:

The access modifier (§6.6) of an overriding or hiding method must provide at least as much access as the overridden or hidden method, or a compile-time error occurs. In more detail:

  • If the overridden or hidden method is public, then the overriding or hiding method must be public; otherwise, a compile-time error occurs.
  • If the overridden or hidden method is protected, then the overriding or hiding method must be protected or public; otherwise, a compile-time error occurs.
  • If the overridden or hidden method has default (package) access, then the overriding or hiding method must not be private; otherwise, a compile-time error occurs.

Note that a private method cannot be hidden or overridden in the technical sense of those terms. This means that a subclass can declare a method with the same signature as a private method in one of its superclasses, and there is no requirement that the return type or throws clause of such a method bear any relationship to those of the private method in the superclass.

は、すべての後、あなただけprivateメソッドを期待する内のコードによって呼び出されます同じクラス - パブリックメソッドをオーバーライドするために呼び出された場合、それはかなり混乱するでしょう。

2

あなたが考えてみれば、これを行うことができるということは理にかなっていない。..

理由は、それは親一つだかのように、周りの子オブジェクトを渡すことができることである(つまり、あなたのように親の型を使用することができますTestClassインスタンスへの参照型)。

parent種類を使用し、そのメソッドを呼び出して別の場所かもしれないいくつかのコードがあり
parent p = new TestClass(); 

例えば

public static void aMethod(parent aParent){ 
    p.func(); 
} 

あなたはメソッドの可視性を低下させることができたならば、aMethod(p)を呼び出すと、ランタイム例外のいくつかの種類をスローしなければならない - これはこれは必須ではありませんが保証できませんが。

関連する問題