1. What is an interface

Interfaces are common specifications for multiple classes.

2. Interface definition

public interfaceThe name of the interface{
    // Interface content
    // jdk1.7 can include constants, abstract methods
    // jdk1.8 can include default, static methods
    Jdk1.9 can include private methods
}
Copy the code

3. Interface usage

There are three main parts to using an interface

(1) You first need to define the interface and the abstract methods in the interface.

(2) Create a class that implements the interface and overwrites all the abstract methods in the interface. Note here that if only the abstract methods of the interface are partially overwritten, then the class needs to be defined as an abstract class.

(3) Create the above class object, you can use the interface.

// 1. Define interfaces and abstract methods in interfaces
public interface MyInterface {
    
    public abstract void method1(a);
    public abstract void method2(a);
}

// 2. Create a new class that implements the interface and overwrites all the abstract methods in the interface
public class MyInterfaceImpl implements MyInterface {
    
    @Override
    public void method1(a) {
        System.out.println("Implement Abstract method 1");
    }
    @Override
    public void method2(a) {
		System.out.println("Implement Abstract Method 2"); }}// 3. Create a class object that can use the contents of the interface
Copy the code

4. Default methods in interfaces

Now suppose that the interface implementation class MyInterfaceImpl is already in use, but now to add a function to the interface, such as an abstract method, MyInterfaceImpl will also need to override the new abstract method, which will cause some trouble. Using the default method in the interface, In this way, the interface upgrade hassle is reduced. For example, we now add a default method, method3(), to the interface, and MyInterfaceImpl doesn’t need to make any changes to access method3() with its objects, making the upgrade insensitive. Of course, we could also do a corresponding upgrade in the implementation class, overriding method3().

public interface MyInterface {

    public abstract void method1(a);

    public abstract void method2(a);
	
    public default void method3(a) {
        System.out.println("Default method in interface"); }}public class InterfaceTest {
    public static void main(String[] args) {
        MyInterfaceImpl myInterface = new MyInterfaceImpl();
        myInterface.method3(); // Print "default method in interface"}}Copy the code

5. Static methods in interfaces

Access to static methods in an interface is directly accessed through the interface, not through the implementation class or the object that implements the class. Another point to note is that while static methods in an interface cannot be overridden by the implementation class, there can be a static method with the same name and argument in the implementation class.

// Static method definition in interface
public interface MyInterface {

    public static void method4(a) {
        System.out.println("Static methods in interfaces"); }}// Implement a static method with the same name and argument in the class
public class MyInterfaceImpl implements MyInterface {

    public static void method4(a) {
        System.out.println("Implement static methods in a class"); }}// static methods are used in the interface
public class InterfaceTest {
    public static void main(String[] args) {
        MyInterface.method4(); // Output "static method in interface"
        MyInterfaceImpl.method4(); // Print "implement static methods in class"}}Copy the code

6. Constants in interfaces

A variable in an interface can only be modified by public static final, so it is a constant. In addition, it is important to note that constants in interfaces are assigned at definition and cannot be changed once assigned.

public interface MyInterface {
	public static final int NUM = 10;
}
Copy the code

7. A class inherits from a parent class and implements multiple interfaces

A class can inherit only one parent class, but can implement multiple interfaces. For example, in the code below, the implementation class inherits from Father and implements interfaces A and B. In this case, it is possible to involve conflicts between interface-interface and interface-parent methods of the same name.

(1) The first kind of conflict is the abstract method conflict between interfaces. It can be seen that both interface A and interface B abstract method methodA(), then the implementation class only needs to rewrite methodA() once.

(2) The second kind of conflict is the default method conflict between interfaces. The default method can be accessed through the implementation class object even if it is not overwritten, but when the default method of two interfaces has the same name, it will not know which one to access, so the default method with the same name must be overwritten in the implementation class.

(3) The third kind of conflict is the default method of the interface and the common method of the parent class conflict, can choose not to rewrite, through the implementation of class object access, call the method in the parent class first; It can also be overridden, then the overridden method is called.

A / / interface
public interface InterfaceA {

    public abstract void methodA(a);

    public abstract void methodB(a);

    public default void methodC(a) {
        System.out.println("Default method C for interface A"); }}B / / interface
public interface InterfaceB {

    public abstract void methodA(a);

    public default void methodC(a) {
        System.out.println("Default method C for interface B");
    }

    public default void methodD(a) {
        System.out.println("Default method D for interface B"); }}/ / parent class
public class Father {

    public void methodD(a) {
        System.out.println("Ordinary method D in the parent class"); }}/ / implementation class
public class InterfaceABImpl extends Father implements InterfaceA.InterfaceB {
    @Override
    public void methodA(a) {
		 System.out.println("Rewriting of abstract methods of the same name");
    }

    @Override
    public void methodB(a) {}@Override
    public void methodC(a) {
        System.out.println("Implementation class default method C"); }}Copy the code

8. Multiple inheritance between interfaces

As mentioned earlier, only single inheritance can be implemented between classes and interfaces, while multiple inheritance can be implemented between classes and interfaces. But there are a few things to note when it comes to multiple inheritance:

(1) The two inherited interfaces, such as interface A and interface B, have the same abstract method methodAbstract(). At this time, in the inherited interface C, there is no need to rewrite the abstract method, but only need to rewrite in the implementation class of interface C.

(2) Two inherited interfaces, such as interface A and interface B, have A default method named methodDefault(), which must be overridden in the inherited interface C.

A / / interface
public interface InterfaceA {

    public abstract void methodA(a);

    public abstract void methodAbstract(a);

    public default void methodDefault(a) {
        System.out.println("Default method for interface A"); }}B / / interface
public interface InterfaceB {

    public abstract void methodB(a);

    public abstract void methodAbstract(a);

    public default void methodDefault(a) {
        System.out.println("Default method for interface B"); }}// Interface C inherits interface A and interface B
public interface InterfaceC extends InterfaceA.InterfaceB {

    public abstract void methodC(a);

    @Override
    default void methodDefault(a) {
        System.out.println("Default method for interface C"); }}Copy the code