What is an interface in Java

The Interfaces are most commonly a group of related methods with no other code or functionality or with the empty bodies. Alternatively, you can say an interface is the blueprint of a class that implements it.

It needs to be differentiated that Java interfaces are not the classes. The Interface contains behaviors that a class implements whereas a class describe the methods (behaviors) and properties (attributes) of the objects.

An example of interface

To make it clearer, consider an example of the Car interface. An interface of the car may look like:

As such, the classes implement the interfaces, let us say a car company, Honda implements this car interface as follows:

So the class will at least implement the basic car requirements and may add its own.

In Java, the List is an interface of the Collection, which is an ordered collection of elements or dynamic array. Whereas the ArrayList, LinkedList classes implements the List interface.

A few main points about interface are:

  • A class implements an interface, not extend it.
  • The Interface can extend many interfaces.
  • Interfaces in java cannot be instantiated.
  • The Java Interfaces do not contain the constructors.
  • It is the mechanism to achieve abstraction in Java (an object-oriented, OOP concept)
  • Methods in interface Java are also abstract implicitly.

How to declare an interface

Enough of the theory, it is time to learn how to declare and run an example of creating the interface.

This is how you can declare an interface:

public interface interface_name


// Final, Static fields here

//Abstract methods go here


An interface example

This example shows how to create a Java interface with one method.

So the test_interface contains one method test(). You have to save this as test_interface.java. Now we will create a class that implements the test_interface. See the code below:

Save this file as interface_example.java.

Experience this online

The output will be:

Hello World

Inheritance in interface

Like classes, the Java interface can also inherit other interfaces. However, unlike the class, an interface in Java can inherit multiple interfaces.

See below, how to inherit single and multiple interfaces.

Single interface inheritance

You have to use the “extends” keyword to inherit the interface.

public interface child_interface extends parent_interface

Multiple interface inheritance

Multiple interfaces are inherited by using a ‘,’ (comma) after first parent class and so on. See below to learn how to extend multiple interfaces.

public interface child_interface extends parent_interface1, parent_interface2

Related reading: Java ArrayList | Java array | Java Vector Class