Interface in Java

Back to home
Logicmojo - Updated Jan 11, 2023



Introduction

An interface is a service requirement specification (srs). From the perspective of the customer, an interface defines the set of services that he expects. An interface defines the collection of services that a service provider offers from the perspective of the service provider. As a result, an interface can be thought of as a contract between a client and a service provider.

Example: The ATM GUI screen defines the set of services that the bank is offering, while also describing the set of services that the consumer is expecting, thereby acting as a contract between the bank and the customer.

Because every method in an interface is always abstract, whether we declare it or not, interface is regarded as a pure abstract class.

An interface is a reference type in Java. It's similar to taking a class. It is made up of a collection of abstract procedures. When a class implements an interface, it inherits the abstract methods from the interface.

In addition to abstract methods, an interface can have constants, default methods, static methods, and nested types. The bodies of default and static methods are the only ones that have them.

In the following ways, an interface is analogous to a class:

  1. Any number of methods can be included in an interface.

  2. An interface is written in a .java file with the same name as the file.

  3. An interface's byte code is stored in a.class file.

  4. Interfaces exist in packages, and their bytecode files must be in the same directory structure as the package name.

An interface, on the other hand, differs from a class in various aspects, including:

  1. You cannot instantiate an interface.

  2. There are no constructors in an interface.

  3. In an interface, all of the methods are abstract.

  4. In an interface, instance fields are not permitted. The only fields that can be used in an interface must be static and final.

  5. Instead than extending an interface, a class implements it.

  6. A single interface can extend many interfaces.

Syntax :

An interface is declared with the interface keyword. By default, all fields in an interface are public, static, and final, and all methods in an interface have an empty body. All of the interface's functions must be implemented by a class that implements the interface.

interface <interface_name>{  
      
    // declare constant fields  
    // declare methods that abstract   
    // by default.  
}  

Why do we use an Interface?

  1. It's a method for completely abstracting something.

  2. Because multiple inheritances in the context of classes are not allowed in Java, multiple inheritances can be obtained by utilising an interface.

  3. It's also suitable for loose coupling.

  4. Abstraction is implemented through interfaces.

Implementation of interface in Java

A class implements an interface in this way. It must provide the body of all interface methods, or in other words, the class must implement all of the interface's methods.

interface MyInterface
{
   /* compiler will treat them as: 
    * public abstract void method1();
    * public abstract void method2();
    */
   public void method1();
   public void method2();
}
class Demo implements MyInterface
{
   /* This class must have to implement both the abstract methods
    * else you will get compilation error
    */
   public void method1()
   {
	    System.out.println("implementation of method1");
   }
   public void method2()
   {
	    System.out.println("implementation of method2");
   }
   public static void main(String arg[])
   {
	    MyInterface obj = new Demo();
	    obj.method1();
   }
}

Advantage of interface in Java

  1. 1. We can accomplish implementation security without worrying about the implementation component.

  2. 2. Several inheritance is not allowed in Java, but you can use interfaces to do so because you can implement multiple interfaces.

  3. 3. Polymorphic behaviour is primarily provided by interfaces.

  4. 4. Interfaces are used to break apart complex designs and make object dependencies explicit.

With this article at Logicmojo, you must have the complete idea of interface in Java.