다음 코드 :왜이 대체 메서드 선언이 컴파일 오류를 생성합니까?
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'에 지워지지만 여러분이 인용 한 스펙은 그들이 공통적으로 지우는 지에 관한 것이 아니라 서브 클래스의 메소드가 수퍼 클래스의 메소드 삭제인지 여부에 관한 것입니다. 부모 클래스가'List'이고 서브 클래스가'List'이면, 괜찮을 것입니다. –
4castle