Interfaces in Java

A class interface can be abstracted fully from its implementation by using the interface keyword. You can specify what a class must do, but not how it does it.

Once an interface is defined, it can be implemented by any number of classes.

1. Defining an Interface

The definition of an interface is similar to a class. It looks as follows:

  • If no access modifier is present then it has default access level. When an interface is declared as public it must be the only interface in that file and its name must match the name of the interface(same convention as classes).
  • Variables are implicitly final and static. So they must be initialized at the time of declaration.
  • All methods and variables are implicitly public.
  • Methods in an interface that are not declared as default or static are implicitly abstract, so the abstract modifier is not used with interface methods. (It can be used, but it is unnecessary.)

Let’s see an example:


2. Implementing an Interface

Once an interface is defined, it can be implemented by any number of classes. We use implements keyword to implement an interface. Also note that a class can implement any number of interfaces. The general form of a class that implements interfaces looks like:

If a class implements more than one interface then the interface names are separated by comma. If two interfaces have the same method declaration of a method then that method has to be implemented only once.

A method that implements an interface method must be declared public and its type signature must match the type signature of the method in the interface.

Output :


3. Interface Reference can access Implementation

We can’t create objects form an interface but we can create variable references. This reference can refer to an object of any class that implements the interface. When a method is called through this reference, it is looked dynamically at runtime and the correct method is called based on the type of object it refers.

Output :

Here we used an interface instead of a class to achieve runtime polymorphism.


4. Interfaces can be partially Implemented

When a class includes an interface but doesn’t fully implement the methods declared inside the interface, it must be declared as abstract.

Output :


5. Interfaces can be extended

We know that a class can extend another class. Similarly an interface can extend another interface. This process can be used to inherit all the members of an already defined interface.

When a class implements an interface that inherits another interface, it must provide implementations for all methods defined within the interface inheritance chain.

Output :


6. An Interface can contain Variables

An interface may contain variables. But they are implicitly final, static and public. Remember that final variables are essentially constants.

Output :


7. Relationship among Interfaces and Classes

The relationship between classes and Interfaces can be visualized as shown in the diagram:

Relationship among interfaces and classes in Java
Relationship among interfaces and classes in Java


8. Multiple Inheritance through Interfaces

Multiple inheritance is not possible through classes but it is possible through interfaces.

  • A class can extend only one classbut it can implement more than one interface at a time.

    Output :

  • An interface can extend more than one interface at a time.

    Output :

Visit this article to know more about multiple inheritance through classes.

9. Miscellaneous topics

  • Nested Interfaces : Interfaces can be nested. An interface can be nested inside another interface or a class. Visit this article to learn more about nested interfaces.

  • Default methods : Starting Java 8 onwards, we can have a default implementation for a method inside the interface.  Such a method must be qualified by the default keyword.

    Output :

  • Static methods in Interfaces : Starting Java 8 onwards, we can have static methods in an interface. A static method must be implemented in the interface where it is declared.

    Output :

  • Tag or Marker Interface : An empty interface is  known as Tag or Marker interface. For example Serializable, Cloneable, Remote are all empty interfaces. They are used to provide some essential information to JVM. Declaration of Serializable interface looks like :

    Visit this article to know more about them.

  • Interfaces vs Abstract Classes : An abstract class provides functionalites similar to an interface but they differ a lot. Visit this article to know the differences and when to use one over the other.