Java Interfaces

An interface is a collection of constants and method declarations. Interfaces make it possible to achieve complete abstraction. An interface is similar to a class, but it lacks instance variables and all methods are declared as abstract methods. In other words, an interface has only abstract methods and constants.

All member variables that exist in an interface are used as constants, and all methods are abstract methods. Therefore, there is no need to add final static to every variable or add the abstract keyword to declared methods. Also, like an abstract class, an interface cannot be instantiated.

Why Use Interfaces?

Why are interfaces used? In Java, class inheritance can have only one parent class. In other words, Java does not support multiple inheritance. By contrast, languages such as C++ allow multiple inheritance. Interfaces are not perfect multiple inheritance, but they can be used to implement something similar to multiple inheritance. Another reason is to unify functionality. There are cases where you want to unify methods across several classes while implementing different behavior. In that case, by using an interface, you can declare the interface as the same data type, create objects from different classes, and call the same method while producing different behavior.

Defining an Interface

An interface is defined similarly to a class.

public interface InterfaceName [extends InterfaceName, ...] {
   // Constant declarations
   // Method declarations
}

All methods in an interface have the public access modifier, so it does not need to be declared separately.

Only variables and methods are declared inside an interface, and all declared variables have final static properties. Methods include the public access modifier by default.

All interfaces cannot be instantiated independently. They must be used through an instance of a derived class after the derived class redefines the interface methods.

Using Interfaces

To use an interface in a class, use the implements keyword. The following is the syntax form of a class that includes an interface. Unlike the extends keyword, implements can list multiple interfaces to implement multiple inheritance-like behavior. If a class implements one or more interfaces, the interfaces are separated by commas.

[public/final/abstract] class ClassName extends ParentClassName implements InterfaceName[, InterfaceName, ...] {
    // Member variables
    // Constructor
    // Methods
    // Must override and declare all methods declared in the interface
}

If an interface is used, all methods defined in that interface must be overridden and implemented inside the class. Also, when redefining interface methods, the public access modifier must be specified.

Inheritance Between Interfaces

Interfaces can inherit other interfaces just like classes. The extends keyword is also used for inheritance between interfaces.

public interface InterfaceName extends InterfaceName[, InterfaceName, ...] {
    // Constant declarations
    // Method declarations
}

Interfaces can have a hierarchy just like classes. A class can inherit only one parent class, but an interface can inherit multiple interfaces.