Skip to content

Instantly share code, notes, and snippets.

@YukiYoshikawa
Last active December 16, 2015 07:59
Show Gist options
  • Save YukiYoshikawa/5402934 to your computer and use it in GitHub Desktop.
Save YukiYoshikawa/5402934 to your computer and use it in GitHub Desktop.
package trial.yy.java8.client.defaultimpl;
/**
* Java8 インターフェースのデフォルト実装を試すためのサンプル
* User: yy
* Date: 13/04/17
* Time: 22:05
*/
public class InterfaceDafaultImplClient {
/**
* デフォルト実装を持つインターフェース
*/
interface SampleInterface {
/**
* デフォルト実装ありのメソッド
* @return 好きな文字列
*/
default String func1() {
return "execute default func1";
}
/**
* デフォルト実装無しのメソッド(今まで通り)
* @return 好きな文字列
*/
String func2();
}
/**
* func2だけをoverrideした実装クラス
*/
static class SampleImpl1 implements SampleInterface {
@Override
public String func2() {
return "exeucute func2";
}
}
/**
* func1,func2両方をoverrideした実装クラス
*/
static class SampleImpl2 implements SampleInterface {
@Override
public String func1() {
return "exeucute func1";
}
@Override
public String func2() {
return "exeucute func2";
}
}
public static void main(String[] args) {
SampleInterface sample1 = new SampleImpl1();
SampleInterface sample2 = new SampleImpl2();
// 各実装クラスをインスタンス化してメソッドの動きを確認
System.out.println("sample1.func1(): " + sample1.func1());
System.out.println("sample1.func2(): " + sample1.func2());
System.out.println("sample2.func1(): " + sample2.func1());
System.out.println("sample2.func2(): " + sample2.func2());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment