public interface Api { | |
// constant declarations | |
int CAFEBABE = 0xCAFEBABE; | |
// abstract methods | |
void foo(int data); | |
void bar(int data); | |
// default methods | |
default void foo2(int data) { | |
new Object() { | |
void foo() { | |
validate(data); | |
System.out.println("I am foo v2!"); | |
} | |
}.foo(); | |
} | |
default void bar2(int data) { | |
new Object() { | |
void bar() { | |
validate(data); | |
System.out.println("I am bar v2!"); | |
} | |
}.bar(); | |
} | |
// static methods | |
static String ping() { | |
return "pong!"; | |
} | |
// private methods | |
private boolean validate(int data) { | |
System.out.println("validating input: " + Integer.toHexString(data) + " => " + this); | |
return true; | |
} | |
static void main(String[] args) { | |
Api.ping(); | |
Api api = new Api() { | |
@Override | |
public void foo(int data) { | |
System.out.println("I am legacy foo!"); | |
} | |
@Override | |
public void bar(int data) { | |
System.out.println("I am legacy bar!"); | |
} | |
}; | |
api.foo(CAFEBABE); | |
api.bar(CAFEBABE); | |
api.foo2(CAFEBABE); | |
api.bar2(CAFEBABE); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment