Interfaces
Variables in Interfaces are by default public static final, meaning they cannot be changed by the implementing class. Methods in Interfaces are by default public abstract.
Default Methods in Intefaces.
The dafault method declaration is preceded by the keyword default modifier.
package main.p3;
public interface SharedConstrant {
default void foo() {
System.out.println("foo");
}
}
It is also possible to intialize static method in Interface.
package main.p4;
public interface A {
default void foo() {
System.out.println("foo in A");
}
static int getInt() {
return 1;
}
}int number = A.getInt();
Remember! static interface methods are not inherited by either an implementing class or a
subinterface.
Beginning with JDK 9, an interface can include a private method. A private interface method
can be called only by a default method or another private method defined by the same interface.
Because a private interface is specified private, it cannot be used by code outside the interface
in which it is defined. This restriction includes subinterfaces because a private interface
method is not inherited by a subinterface. The key benefit of a private interface method is that
it lets two or more default methods use a common piece of code, thus avoiding code duplication.
Комментарии
Отправить комментарий