Before Java8, all methods in the Interface Interface were abstract methods and constants. So what’s new with Interface in Java8?

Static members

Before Java8, we would define constants. We would write a class containing final static variables like this:

public class Constants {
	public static final int MAX_SERVICE_TIME = 100;
}

public class Hello {
	public static void main(String[] args) { System.out.println(Constants.MAX_SERVICE_TIME); }}Copy the code

In Java8, Interface supports static members. Members are public final static by default and can be called directly from outside the class.

public interface MyInterface {
	int MAX_SERVICE_TIME = 100;
}

public class Hello {
	public static void main(String[] args) { System.out.println(MyInterface.MAX_SERVICE_TIME); }}Copy the code

The default function

Before Java8, functions in Interface were not implementable as follows:

public interface MyInterface {
	int MAX_SERVICE_TIME = 100;

	void test(a);
}
Copy the code

In Java8, Interface supports the implementation of functions, as long as the function is preceded by the default keyword, as follows:

public interface MyInterface {
	int MAX_SERVICE_TIME = 100;

	void test(a);
	
	default void doSomething(a) {
		System.out.println("do something"); }}Copy the code

Default function, the implementation class can not implement this method, if you do not want to subclass to implement some methods, can be written default function.

Before Java8, if we wanted to implement this functionality, there was a way to define Interface, then define Abstract Class to implement Interface, and then define Class to inherit Abstract Class. So that Class doesn’t have to implement all the methods in Interface.

The static function

In Java8, Interface is allowed to define static methods. This allows API designers to define static utility methods like getInstance in the Interface, which makes the API concise and concise, as follows:

public interface MyInterface {
	default void doSomething(a) {
		System.out.println("do something"); }}public class MyClass implements MyInterface {}public interface MyClassFactory {
	public static MyClass getInstance(a) {
		return newMyClass(); }}Copy the code

@ FunctionalInterface annotations

  • What is theFunctional interface?

A functional interface is essentially an interface, but it is a special kind of interface: a SAM type interface (Single Abstract Method). This type of interface is defined so that methods that take it as an argument can be called with a Lambda expression as an argument.

@FunctionalInterface
public interface MyInterface {
	void test(a);
}
Copy the code

The @functionalinterface annotation helps us detect whether an Interface is a FunctionalInterface, but it is optional and will not fail.

@FunctionalInterface
public interface MyInterface {/ / an error
	void test(a);

	void doSomething(a);
}
Copy the code

The non-default/static methods in Interface are abstract, so the above Interface is not a FunctionalInterface, adding @functionalinterface to it tells us that it is not a FunctionalInterface.

  • What does a functional interface do?

A functional interface that can be called with a lambda expression as an argument.

So, the next Java8 feature is Lambda expressions, which we’ll explain in the next section.

Code on among