Javaã®å¤ãã¢ã¬ãªãã©ã¯ãã£ã¹ã®ï¼ã¤ã«ãã¦ã¼ãã£ãªãã£ã¯ã©ã¹*1ãabstractã§ä½æããã¨è¨ãã®ãããã
public abstract class SomeUtil { public static String appendA(String str) { return str + "a"; } }
ã¦ã¼ãã£ãªãã£ã¡ã½ããappendAã¯ãç¹ã«å½¹ã«ç«ããªãããå¼æ°ã®Stringã«Aã足ãã¦è¿ãç©ä½ã ããã®ã¯ã©ã¹ã«ã¯staticã¡ã½ããããå«ã¾ãªããããã¤ã³ã¹ã¿ã³ã¹åããå¿ è¦ã¯ãªããããããã¯ã©ã¹ãabstractã«ãããã¨ã«ããã誤ã£ã¦ã¤ã³ã¹ã¿ã³ã¹åããããã¨ã¯ç¡ãã¨è¨ãç´ æ´ãããæ¹æ³ã§ããã
ããã¼ããã¨è¨ãè¨èã¯å ªãã¦é ããããããã§ã¯è©±ãçµãã£ã¦ãã¾ãã
ãã¦ãJavaSE8ã§ã¯Project Lambdaã®ã¤ãã§ã«ã¤ã³ã¿ãã§ã¼ã¹ã«defaultã¡ã½ããã追å ããããã¤ãã§ã«staticã¡ã½ãããæ¸ããããã«ãªã£ãããªãstaticãæ¸ããããã«ãªã£ããã¯ç¥ããªãã
ä»ã¾ã§abstractãªã¦ã¼ãã£ãªãã£ã¯ã©ã¹ãæ¸ãã¦ãã人ã«æå ±ã ãããããã¯interfaceã«æ¸ãã°è¯ãã"abstract class"ã¨è¨è¿°ãã¦ããã®ã"interface"ã§è¯ããªãã14æåãã9æåã«ãªãããªãã¨çç£æ§ã150%ã«ãªããç´ æ´ãããã
ããã¾ã¦ãinterfaceã«è¨è¿°ã§ããã¨è¨ããã¨ã¯ãå¤ãã¢ã¬ãªãã©ã¯ãã£ã¹ã®ï¼ã¤ãã¤ã³ã¿ãã§ã¼ã¹ã«staticååä»ãå®æ°ãå®ç¾©ãã¦importãããã¨ã«ãããã¯ã©ã¹ã§ä½¿ç¨ããã¨ãã«ä¿®é£¾ããªãã¦è¯ããªãã¨è¨ããã®ãããã*2 *3
staticã¡ã½ããã§ãããã¯ä½¿ããã®ã§ã¯â¦â¦ããï¼ï¼
public class Main implements ISomeUtil { public static void main(String... args) { System.out.println(hello()); } } interface ISomeUtil { public static String hello() { return "HELLO"; } }
å³åº§ã«ã³ã³ãã¤ã«ï¼
% javac Main.java Main.java:3: ã¨ã©ã¼: ã·ã³ãã«ãè¦ã¤ãããã¾ãã System.out.println(hello()); ^ ã·ã³ãã«: ã¡ã½ãã hello() å ´æ: ã¯ã©ã¹ Main ã¨ã©ã¼1å
â¦â¦(´ã»Ïã»ï½)
ãªããã¡ããã¨"ISomeUtil.hello()"ã¨ãæå®ããªãã¨å¼ã¹ãªãã¿ãããstaticãã£ã¼ã«ãã¯ããã®ã«ã
ã¾ãã§ãã¦ãå¬ããç¡ãã®ã§ãã§ããªãã¦ãããã©ã
é¢ä¿ãããã§ããªãdefaultã¡ã½ããã®Diamondç¶æ¿ã«ã¤ãã¦æãããã㤠â DiamondProblemの対応で気になったこと - 日々常々
*1:ããã§ã¯staticã¡ã½ããã®ã¿ãå®ç¾©ããã¯ã©ã¹ãã¦ã¼ãã£ãªãã£ã¯ã©ã¹ã¨è¨ã£ã¦ãããã
*2:interfaceã¯è¤æ°implementsã§ããããããããè¡ã£ã¦ãä½ããæãªãããã§ã¯ãªãã
*3:static importã®å°å ¥ã§ãããã¢ã¬ããå¢ãããã®ã