4. Static Methods
//No different from static methods from classes
public interface StaticFoo {
static void foo() {
System.out.println("Static implementation of foo");
}
}
10. Default
Methods
public interface Foo {
public default void foo() {
System.out.println("Default implementation of foo()");
}
}
Java 8 – Basic Syntax
11. Default
Methods
public interface Itf {
//No implementation; will be overridden
public void foo();
//Default implementation; will be overriden
public default void bar() {
System.out.println("Itf -> bar() [DEFAULT]");
}
//Default implementation; will not be overriden
public default void baz() {
System.out.println("Itf -> baz() [DEFAULT]");
}
}
Java 8 – Call &Overload
12. Default
Methods
public class Clazz implements Itf {
@Override
public void foo() {
System.out.println("Clazz -> foo()");
}
@Override
public void bar() {
System.out.println("Clazz -> bar()");
}
}
Java 8 – Call &Overload
13. Default
Methods
public class Test {
public static void main(String[] args) {
Clazz clz = new Clazz();
clz.foo();
clz.bar();
clz.baz();
}
}
Java 8 – Call &Overload
Clazz -> foo()
Clazz -> bar()
Itf -> baz() [DEFAULT]
15. Default
Methods
public interface InterfaceA {
public default void foo() {System.out.println("A -> foo()");}
}
public interface InterfaceB {
public default void foo() {System.out.println("B -> foo()");}
}
public class Test implements InterfaceA, InterfaceB {
//Whoops
}
Diamond Inheritance
error: classTest inherits unrelated defaults for foo()
from types InterfaceA and InterfaceB
16. Default
Methods
//Solution 1 : the class method has the priority
public class Test implements InterfaceA, InterfaceB {
@Override
public void foo() {
System.out.println("Test -> foo()");
}
}
Diamond Inheritance
17. Default
Methods
//Solution 2 : we specify which method we want to call
public class Test implements InterfaceA, InterfaceB {
@Override
public void foo() {
InterfaceA.super.foo();
}
}
Diamond Inheritance
22. Default
Methods
When two or more interfaces of a proxy class contain a method
with the same name and parameter signature, the order of the
proxy class's interfaces becomes significant. When such a duplicate
method is invoked on a proxy instance, the Method object passed to
the invocation handler will not necessarily be the one whose declaring
class is assignable from the reference type of the interface that the
proxy's method was invoked through.This limitation exists because
the corresponding method implementation in the generated proxy
class cannot determine which interface it was invoked through.
Therefore, when a duplicate method is invoked on a proxy instance,
the Method object for the method in the foremost interface that
contains the method (either directly or inherited through a
superinterface) in the proxy class's list of interfaces is passed to the
invocation handler's invoke method, regardless of the reference type
through which the method invocation occurred.
java.lang.reflect.Proxy, 2014
Reflection & Proxies
27. Traits
public interface Orderable<T> extends Comparable<T> {
//everything relies on the implementation of compareTo
public default boolean isAfter(T other) {
return compareTo(other) > 0;
}
public default boolean isBefore(T other) {
return compareTo(other) < 0;
}
public default boolean isSameAs(T other) {
return compareTo(other) == 0;
}
}
28. Traits
public class Person implements Orderable<Person> {
private final String name;
public Person(String name) {
this.name = name;
}
@Override
public int compareTo(Person other) {
return name.compareTo(other.name);
}
}
29. Traits
public class Test {
public static void main(String[] args) {
Person tony = new Person("Tony Stark");
Person bruce = new Person("Dr. Robert Bruce Banner");
println("Tony compareto Bruce : " + tony.compareTo(bruce));
println("Tony > Bruce : " + tony.isAfter(bruce));
println("Tony < Bruce : " + tony.isBefore(bruce));
println("Tony == Bruce : " + tony.isSameAs(bruce));
}
}
Tony compareTo Bruce : 16
Tony > Bruce : true
Tony < Bruce : false
Tony == Bruce : false
33. Functional
Interfaces
@FunctionalInterface
public interface Foo {
void foo();
void bar(); // whoops, one too many
}
one abstract / many defaults…
error: Foo is not a functional interface multiple non-overriding
abstract methods found in interface Foo
1 error
38. Functional
Interfaces
public static void main(String[] args) {
NameParser<Name> parser = new NameParser();
Name res = parser.parse("Bruce Banner", new Creator<Name>() {
@Override
public Name create(String firstName, String lastName) {
return new Name(firstName, lastName);
}
});
}
Java 7
41. Functional
Interfaces
public class Factory {
public static Name createName(String firstName, String lastName) {
return new Name(firstName, lastName);
}
}
Name res = parser.parse("Bruce Banner", Factory::createName);
Java 8
STATICMETHOD
42. Functional
Interfaces
public class Factory {
public Name createName(String firstName, String lastName) {
return new Name(firstName, lastName);
}
}
Factory factory = new Factory();
Name res = parser.parse("Bruce Banner", factory::createName);
Java 8
INSTANCEMETHOD