Java Interface

An interface is a reference type in Java. It is similar to class. It is a collection of abstract methods. A class implements an interface, thereby inheriting the abstract methods of the interface. Along with abstract methods, an interface may also contain constants, default methods, static methods, and nested types (TutorialsPoint 2022).

Summary

In versions of Java 9 and above, interface can include:

1: Member Variable (Constant)

[public] [static] [final] (Type) (VariableName) = (value);

Properties:
1: Constants must be assigned by values and can not be changed in the program.
2: Use all capital letters to name variables and use underscores (_) to separate them.

2: Abstract Method

[public] [abstract] (Type) (MethodName) (Parameter List);

Property
1: Implementing class must override all abstract methods of the interface unless the class is an abstract class.

3: Default Method

[public] default (Type) (MethodName) (Parameter List) {method}

Property
1: Default methods can also be overridden.

4: Static Method

[public] static (Type) (MethodName) (Parameter List) {method}

Property
1: Static methods should be invoked by the name of the interface rather than the Implementing object.

5: Private Method

Private Method

private (Type) (MethodName) (Parameter List) {method}

Static Private Method

private static (Type) (MethodName) (Parameter List) {method}

Property
1: Only interface itself can invoke its own private methods.


General Properties of using interfaces

1: There is no static code block or constructor inside of an interface.
2: An implementing class can implement multiple interfaces.

public class MyInterfaceImpl implements MyInterfaceA, MyInterfaceB {
    // override all abstract methods from MyInterfaceA and MyInterfaceB
}

3: If there are identical abstract methods in multiple interfaces implemented by the implementing class, they only need to be implemented once.
4: If the implementing class does not override all abstract methods of interfaces, then the implementing class must be an abstract class.
5: If there are identical default methods in multiple interfaces implemented by the implementing class, we must override conflicting default methods.
6: If there are conflicts between the method from the base class and the default method from the interface, the base class method will be invoked.
7: An interface can inherit multiple interfaces.
8: If there are identical default methods in multiple base interfaces, we must override the default method on the derived interface.

@Override
public default void methodDefault (Parameter List) {method} // cannot omit 'default' keyword

Examples of using interfaces

1: Member Variable (Constant)

We can define member variables inside of an interface but they will be modified with ‘public static final’, therefore, those member variables are equivalent to constants.

// Interface
public interface MyInterfaceConst {
    // We can omit 'public static final' keywords.
    // We have to assign a value to the member variable.
    // Use all capital letters to name variables and use underscores (_) to separate them.
    public static final int NUM_OF_VAL = 10; // It is technically a constant.
}

// Invoke the interface
public class Demo {
    public static void main(String[] args) {
        // Using the name of the interface to access the member variables inside of the interface
        System.out.println(MyInterfaceConst.NUM_OF_VAL);
    }
}

2: Abstract Method

// Interface
public interface MyInterfaceAbstract {
    // The following statements are all grammatically correct.
    public abstract void methodAbs1();
    abstract void methodAbs2();
    public void methodAbs3();
    void methodAbs4();
}  

// Implementing class
public class MyInterfaceAbstractImpl implements MyInterfaceAbstract {
    @Override
    public void methodAbs1() {
        // Implementation
    }

    @Override
    public void methodAbs2() {
        // Implementation
    }

    @Override
    public void methodAbs3() {
        // Implementation
    }

    @Override
    public void methodAbs4() {
        // Implementation
    }
}

// Using implementing class to invoke interface
public class Demo {
    public static void main(String[] args) {
        MyInterfaceAbstractImpl impl = new MyInterfaceAbstractImpl();
        impl.methodAbs1();
        impl.methodAbs2();
        impl.methodAbs3();
        impl.methodAbs4();
    }
}

3: Default Method

Usage: Default method is used to resolve the issue when upgrading the interface. When we want to add a new method in different interfaces that inherit from a base interface, we do not need to add it to each derived interfaces, instead we just need to define a default method in the base interface.

// Base interface
public interface MyInterfaceDefault {
    public default void methodDefault() {
        // We can omit 'public' but cannot omit 'default' keyword.
        // Implementation
    }
}

// Implementing classes
public class MyInterfaceDefaultImpl1 () {
    // Already contained the default method
}

public class MyInterfaceDefaultImpl2 () {
    // Already contained the default method
    // But can still be overridden 
    @Override 
    public void methodDefault() {
        // New Implementation
    }
}

// Using implementing class to invoke interface
public class Demo {
    public static void main(String[] args) {
        // Even though there is no method called 'methodDefault' in the first implementing class, but we can still invoke this method.
        MyInterfaceDefaultImpl1 impl1 = new MyInterfaceDefaultImpl1();
        impl1.methodDefault();
        
        // The default method has been overridden.
        MyInterfaceDefaultImpl2 impl2 = new MyInterfaceDefaultImpl2();
        impl2.methodDefault();
    }
}

4: Static Method

// Interface
public interface MyInterfaceStatic {
    public static void methodStatic() {
        // We can omit 'public' but cannot omit 'static' keyword.
        // Implementation
    }
}

// Implementing class
public class MyInterfaceStaticImpl implements MyInterfaceStatic {
    // Leave it empty
}

// Using implementing class to invoke interface
public class Demo {
    public static void main(String[] args) {
        // MyInterfaceStaticImpl impl = new MyInterfaceStaticImpl(); 
        // impl.methodStatic();
        // Wrong way to invoke static method (We cannot use the implementing object to invoke the static method.)
        
        MyInterfaceStaticImpl.methodStatic();
        // Correct way to invoke static method (by using the name of the interface)
    }
}

5: Private Method

Private Method: Resolve the issue of duplicated code between default methods

// Interface
public interface MyInterfacePrivate {
    public default void methodDefault1() {
        // Its own Implementation
        methodCommon();
    }

    public default void methodDefault2() {
        // Its own implementation
        methodCommon();
    }

    private void methodCommon() {
        // Common implementation of methodDefault1 and methodDefault2
        // Avoid the implementing object of the interface using this private method
    }
}

Static Private Method: Resolve the issue of duplicated code between static methods

// Interface
public interface MyInterfaceStaticPrivate {
    public static void methodStatic1() {
        // Its own Implementation
        methodStaticCommon();
    }

    public static void methodStatic2() {
        // Its own implementation
        methodStaticCommon();
    }

    private static void methodStaticCommon() {
        // Common implementation of methodStatic1 and methodStatic2
        // Avoid this private static method being used 
    }
}

// Invoke the interface
public class Demo {
    public static void main(String[] args) {
        MyInterfaceStaticPrivate.methodStatic1();
        MyInterfaceStaticPrivate.methodStatic2();
    }
}

Reference

  1. Tutorial Points 2022, Java - Interfaces, Tutorial Points, viewed 13 November 2022, https://www.tutorialspoint.com/java/java_interfaces.htm#.
  2. ITHeima 2019, 黑马最新JavaSE零基础入门到入土(idea版), online video, Bilibili, 6 February, 14 November 2022, https://www.bilibili.com/video/BV1Yb411z7PG/?p=181&spm_id_from=pageDriver&vd_source=b8413d3b7040957c6455a2e0ced4d206.