Interface in Java

Rumman Ansari   Software Engineer   2023-05-09   8713 Share
☰ Table of Contents

Table of Content:


Interface looks like class but it is not a class or we can say an interface in java is a blueprint of a class also an interface is a reference type in Java. It has static constants and abstract methods.

The interface in java is a mechanism to achieve abstraction. There can be only abstract methods in the java interface, not method body. It is used to achieve abstraction as well as multiple inheritance in Java.

Writing an interface is similar to writing a class. But a class describes the attributes and behaviors of an object. And an interface contains behaviors that is actually idea of interface and these idea is construct in a class by implementation.

An interface can have methods and variables, static methods, and nested types. Just like the class but the methods declared in interface are by default abstract (only method signature, no body). Also, the variables declared in an interface are public, static & final by default. We will discuss these points in detail, later in this tutorial.

Java Interface also represents IS-A relationship.

It cannot be instantiated just like abstract class.

What is the use of interfaces?

  • As mentioned above they are used for abstraction.
  • Since methods in interfaces do not have a body, they have to be implemented by the class before you can access them. The class that implements an interface must implement all the methods of that interface. Also, Java programming language does not support multiple inheritances, using interfaces we can achieve this as a class can implement more than one interfaces, however, it cannot extend more than one classes.
  • It can be used to achieve loose coupling.

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

interface in java

Properties of Interfaces in Java

  1. Interfaces are implicitly abstract
  2. Each and every method within an interface is also implicitly abstract
  3. For the above reason, methods do not need to be declared abstract
  4. All methods of an interface are implicitly public
  5. Interface adds public, static and final keywords before data members.
  6. For the above reason, data members do not need to be declared public, static and final.

Understanding relationship between classes and interfaces

interface in java

Declaration
Interfaces are declared by specifying a keyword interface. E.g.:

 //multiple import statements can be used here
public interface IntrfaceName
{
//Any number of final, static fields
//Any number of abstract method declarations
}
interface MyInterface
{
   /* All the methods are public abstract by default
    * Note down that these methods are not having body
    */
   public void method1();
   public void method2();
}

Interface Implementation

Once an Interface has been defined, one or more classes can implement that interface. For implementing an interface, programmers need to use the clause ‘implements’ in the class definition and then create the method defined by the interface.

This is how a class implements an interface. It has to provide the body of all the methods that are declared in interface.
Note: class implements interface but an interface extends another interface.

Example of Java Interface

Program:

n this example, Printable interface has only one method, its implementation is provided in the Message class.

interface Msg{
void print();
}
class Message implements Msg{
public void print(){
	System.out.println("welcome and Hello!");
	}

public static void main(String args[]){
Message obj = new Message();
obj.print();
 }
}
Output:
welcome and Hello!
Press any key to continue . . .

Another example of Java Interface

Program:
interface MyInterface
{
   public void method1();
   public void method2();
}

class ImpInterface implements MyInterface
{
  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 ImpInterface();
      obj. method1();
      obj. method2();
  }
}
Output:
implementation of method1
implementation of method2
Press any key to continue . . .

Another example of Java Interface

Program:

Two different implementation in different class


In this example, SendSms interface has only one method. Its implementation is provided by Ramboo and Rahim classes. In real scenario, interface is defined by someone but implementation is provided by different implementation providers. And, it is used by someone else. The implementation part is hidden by the user which uses the interface.

//Interface declaration: by first user
interface SendSms{
void message();
}

//Implementation: by second user
class Ramboo implements SendSms{
	public void message(){
		System.out.println("Hello by Ramboo");
		}
}

class Rahim implements SendSms{
public void message(){
	System.out.println("Hello by Rahim");
	}
}

//Using interface: by third user
class MainClassInterface{
	public static void main(String args[]){
	SendSms d=new Rahim();
	d.message();
	}
}
Output:
Hello by Rahim
Press any key to continue . . .