What is Interface in Java? Usage and Implementation

An interface is similar to a class in Java. It is a collection of abstract method and constant fields. Sometimes it is called as reference type in java. Except abstract methods and constants, it can contain default method, static method and nested types.

Why we to use interface in java?

The interface in java is a mechanism to achieve abstraction. It is used to achieve abstraction and multiple inheritance in Java. There can be three reasons that we should use interface.

  1. It is used to achieve abstraction.
  2. By interface, we can support the functionality of multiple inheritance.
  3. It can be used to achieve loose coupling.

 Relating unrelated types:

An interface is often used when disparate (i.e., unrelated) classes need to share common methods and constants. This allows objects of unrelated classes to be processed polymorphic-ally —objects of classes that implement the same interface can respond to the same method calls. You can create an interface that describes the desired functionality, then implement this interface in any classes that require that functionality. For example, in the accounts payable application developed in this section, we implement interface Payable in any class that must be able to calculate a payment amount (e.g., Employee, Invoice).

Class vs. Interface:


Interface and Class
Interface and Class

An interface is different from a class in several ways, some are listed below:

  • You cannot instantiate an interface.
  • An interface does not contain any constructors.
  • All of the methods in an interface are abstract.
  • An interface cannot contain instance fields. The only fields that can appear in an interface must be declared both static and final.
  • An interface is not extended by a class; it is implemented by a class.
  • An interface can extend multiple interfaces.

However, an interface is similar to a class in the following ways:

  • An interface can contain any number of methods.
  • An interface is written in a file with a .java extension, with the name of the interface matching the name of the file.
  • The byte code of an interface appears in a .class file.
  • Interfaces appear in packages, and their corresponding bytecode file must be in a directory structure that matches the package name.

Abstract Classes vs. Interfaces:

An interface is often used in place of an abstract class when there’s no default implementation to inherit—that is, no fields and no default method implementations. Like public abstract classes, interfaces are typically public types. Like a public class, a public interface must be declared in a file with the same name as the interface and the .java file-name extension.

Tagged Interfaces:

An interface that have no member is known as marker or tagged interface. For example: Serializable, Clone able, Remote etc. They are used to provide some essential information to the JVM so that JVM may perform some useful operation.

Declaring an interface:

Declaring an interface is easy just like methods, classes; the interface keyword is used to declare an interface. Concentrate on the example below:

The java compiler adds public and abstract keywords before the interface method. More, it adds public, static and final keywords before data members.

Using or Implementing an Interface:

To use an interface, a concrete class must specify that it implements the interface and must declare each method in the interface with the signature specified in the interface declaration. To specify that a class implements an interface add the implements keyword and the name of the interface to the end of your class declaration’s first line. A class that does not implement all the methods of the interface is an abstract class and must be declared abstract. Implementing an interface is like signing a contract with the compiler that states, “I will declare all the methods specified by the interface or I will declare my class abstract.”


Above code outputs:

1- Books
2- Tutorial Point

This simple article ends here, Please share your ideas after reading the article. stay tuned with CodeJow.

Leave a Reply