Interface declaration

public interface GroupedInterface extends Interface1, Interface2, Interface3 {
 
    // constant declarations
    
    // base of natural logarithms
    double E = 2.718282;
 
    // method signatures
    void doSomething (int i, double x);
    int doSomethingElse(String s);
}

Interface body

Methods

  • All methods in an interface are implicitly public and abstract, so you can omit them.
  • The interface body can contain:
    • public abstract methods without implementation (keywords can be omitted)
    • public default methods with implementation
    • public static methods with implementation
    • private methods with implementation (Java 9+)
    • private static methods with implementation (Java 9+)
  • The only methods that have implementations are default and static methods.
  • The only access specifier other than public which is allowed is private since Java 9 version but you must provide implementation
  • Interface can’t contain constructors
    • Hence no instance initializer block
    • They cannot have static initialization block, since interfaces should not have any side effects

Constants

  • All constant values defined in an interface are implicitly public, static, and final. It is permitted to redundantly specify any or all of these modifiers.
  • You cannot have any other access/non-access specifiers other than the above
  • Classes can have fields whereas interfaces cannot.

All possible fields/methods in interface

interface Interface {
        
    int INT_CONSTANT = 0; // it's a constant, the same as public static final int INT_CONSTANT = 0
 
    void instanceMethod();  // An abstract method
 
    static void staticMethod() {
        System.out.println("Interface: static method");
    }
 
    // Java 9+, seems like private added to static methods
    private static void staticPrivateMethod() {
        System.out.println("Interface: private static method");
    }
 
    default void defaultMethod() {
        System.out.println("Interface: default method. It can be overridden");
    }
 
    // Java 9+, seems/looks like private added to default methods
    private void privateMethod() {
        System.out.println("Interface: private methods in interfaces are acceptable but should have a body");
    }
}

Casting Interface and using as type

  • When you define a new interface, you are defining a new reference data type. If you define a reference variable whose type is an interface, any object you assign to it must be an instance of a class that implements the interface.
  • As Type: Map is an interface, HashMap is a class
Map<String, Integer> bookStore = new HashMap<>();
  • We can cast as follows:
Relatable obj1 = (Relatable)object1;

Empty Interface

  • Empty interfaces (interfaces without body) can be used as types and to mark classes without requiring any particular method implementations.
  • For an example of a useful empty interface, see java.io.Serializable.

Marker interfaces

  • aka marker or tagged interfaces
  • Interfaces without body
  • denotes something meaningful about the class that implements it
  • Examples:
    • Serializable
    • Clonable
    • Remote etc.

Functional Interface

Why static and default introduced in Java 8?