2ã¤ã®ã¤ã³ã¿ã¼ãã§ã¼ã¹(interfaceA, interfaceB)ãå®è£ ããã¯ã©ã¹ TestAandB ãèãã¾ãã
interface interfaceA {} interface interfaceB {} class TestAandB implements interfaceA, interfaceB {}
interfaceAåã®ãªãã¸ã§ã¯ããå¼æ°ã«åãé¢æ° test(interfaceA iA) ãå®ç¾©ãã¾ãã
public static void test(interfaceA iA) { System.out.println("interfaceA"); }
以ä¸ã®ããã«mainé¢æ°ãå®è¡ããã¨ã"interfaceA"ã¨åºåããã¾ãã
public class Main { public static void main(String[] args) { TestAandB obj = new TestAandB(); test(obj); } public static void test(interfaceA iA) { System.out.println("interfaceA"); } }
次ã«ãinterfaceBåã®ãªãã¸ã§ã¯ããå¼æ°ã«åãé¢æ° test(interfaceB iB) ã追å ã§å®ç¾©ãã¾ããï¼ãªã¼ãã¼ãã¼ãï¼
public class Main { public static void main(String[] args) { TestAandB obj = new TestAandB(); test(obj); } public static void test(interfaceA iA) { System.out.println("interfaceA"); } public static void test(interfaceB iB) { System.out.println("interfaceB"); } }
ãã®ã¨ããtesté¢æ°ã¯ã©ã¡ããå®è¡ãããã®ã§ããããï¼
å®ã¯ããã®ã³ã¼ãã¯ã³ã³ãã¤ã«ã§ãã¾ããã
Exception in thread "main" java.lang.Error: Unresolved compilation problem: The method test(interfaceA) is ambiguous for the type Main
ã©ã¡ãã®testé¢æ°ãå¼ã¹ã°ããã®ãåãããªãã®ã§ã"ambiguous"ã ã¨è¨ã£ã¦ããã¾ãã
ä»åã¯åç´ãªä¾ã§ãããã親ã¯ã©ã¹ã®æ¹ã§ã¤ã³ã¿ã¼ãã§ã¼ã¹ãå®è£ ãã¦ãããã¨ã«æèãåãã¦ããªãã£ããããã¨ããã®ãããªã³ã¼ããæ¸ãã¦ãã¾ã(?)ãããããªãã§ããããã