Interface

The interface keyword is used to declare an interface.

The implements keyword specifies a list of interfaces, that class implements.

The default keyword allows to specify method with default implementation. Methods without implementation are called abstract. A non-abstract class must implement all abstract methods.

By default methods of interface are public. Since Java 9 interface can have private non-abstract method.

An interface can extend multiple interfaces. And an interface can not extend a class.

Empty interface is used as label or marker. For example, the Cloneable interface notes that calling the clone() method is valid. Otherwise, a CloneNotSupportedException will be thrown.

Like abstract classes, interfaces can be nested and have an anonymous implementation.

public interface MyInterface extends CharSequence, Cloneable {
    void method1();
    void method2();
    
    private void method3(){
        
    }
    
    default void method4(){
        
    }
}
public class Demo implements MyInterface{ @Override public void method1() { } @Override public void method2() { } @Override public int length() { return 0; } @Override public char charAt(int index) { return 0; } @Override public CharSequence subSequence(int start, int end) { return null; } // ... }
// anonymous implementation CharSequence charSequence = new CharSequence() { @Override public int length() { return 0; } @Override public char charAt(int index) { return 0; } @Override public CharSequence subSequence(int start, int end) { return null; } };