次のコード:なぜこのオーバーライドメソッド宣言はコンパイルエラーを生成しますか?
class Parent {
public void method(List parameter){
}
}
class Child extends Parent {
public void method(List<String> parameter) {
}
}
は、次のエラーでコンパイルに失敗します。
Parent.java:12: error: name clash: method(List<String>) in Child and method(List) in Parent have the same erasure, yet neither overrides the other
public void method(List<String> parameter) {
^
1 error
しかし、私は§8.4.8.1でJLS8が言うことを確認しています:
An instance method mC declared in or inherited by class C, overrides from C another method mA declared in class A, iff all of the following are true:
...
The signature of mC is a subsignature (§8.4.2) of the signature of mA.
...
そして、 §8.4.2でそれは言う:
The signature of a method m1 is a subsignature of the signature of a method m2 if either:
...
the signature of m1 is the same as the erasure (§4.6) of the signature of m2.
この場合、元の宣言と上書きする宣言の両方が同じ消去を持つため、なぜコンパイルが失敗するのですか?
'List'は 'List'の消去ではないので、引用した仕様ではサブシグニチャではありません。私はあなたが 'm1'と' m2'を混ぜ合わせたと思います。 –
4castle
彼らは両方ともリスト
これらは両方とも型リストから消去されていますが、あなたが引用した仕様は共通の消去があるかどうかではなく、サブクラスのメソッドがスーパークラスメソッドの消去であるかどうかについてです。親クラスに 'List'があり、そのサブクラスに 'List'があった場合、あなたはうまくいくでしょう。 –
4castle