2017-04-13 19 views
1

두 인터페이스 (특히 Serializablejava.security.Key 인터페이스)를 구현하는 객체가 있습니다. 이 객체의 클래스에는 두 개의 정적 메서드도 있습니다. 하나는 Serializable 객체이고 다른 하나는 Key 객체입니다. 나는 위의 main 메소드를 실행하면 이제 오류가 발생하지 및 Java는 test(Key) 메소드를 호출한다Java - 오버로드 된 메소드는 인수 용 인터페이스를 사용합니다. 어떤 방법이 호출되며 그 이유는 무엇입니까?

public class Subclass implements Serializable, java.security.Key{ 
    public static void test(Serializable s) { 
     System.out.println("Ser"); 
    } 
    public static void test(java.security.Key k) { 
     System.out.println("Key"); 
    } 

    public static void main(String[]args){ 
     test(new Subclass()); 
    } 

} 

: 여기에 몇 가지 예제 코드입니다. 내가 알고 싶은 무엇

The method test(Key) is ambiguous for the type SubClass .

:하지만 난 뭔가에 Serializable 인터페이스를 변경하면, (나는 test(Serializable)의 매개 변수를 변경하고 Subclass의 슈퍼 인터페이스 변경) 뭔가 다른, 내 IDE 나에게 오류를 보여줍니다 그렇다면 Java가 Serializable 인터페이스를 사용하는 이런 종류의 메소드 모호성을 허용하는 이유는 무엇입니까? java.security.Keyjava.security.KeySerializable보다 전문화 이고 메소드 호출에 바람직 따라서 Serializable 자체 연장 때문에

+3

다른 유형은 무엇을 사용 했습니까? 이러한 다른 * * –

답변

6

이 작동하고 test(java.security.Key k) 호출되는 이유이다.

+0

https://docs.oracle.com/javase/specs/jls/se8/html/jls-15.html#jls-15.12.2.5 –