Interface in java

Today we are going to learn interface in java.

Interface is a collection of abstract methods and static constants. These abstract methods have only declaration without definition.

Also read – learn abstraction in java

A class which implements interface should define each and every method in the class. Interface is used to implement multiple inheritance in java.


Declaring an interface

Syntax

interface
{
   // fields are static, final and public
   // methods are abstract and public  
}

Example

In interface Vehicle we have abstract method accelerate() and implementation is provided by Audi and BMW class like this,

interface Vehicle
{ 
   public void accelerate(); 
}

class BMW implements Vehicle
{ 
   public void accelerate()
   {
      System.out.println("BMW accelerating...");
   } 
}

public class InterfaceDemo 
{
   public static void main(String[] args) 
   {
      Vehicle obj = new Audi();
      obj.accelerate();
   }
}

Output:

BMW accelerating…

In the above example we have declared Vehicle interface and we can see the implementation in the class BMW.

The relationship between a class and interface is a class implements an interface, an interface extends interface and a class extends another class.


Multiple inheritance is not supported in java. Why?

class X
{
   public void print()
   {
      System.out.println("class X method");
   }
}
class Y
{
   public void print()
   {
      System.out.println("class Y method");
   }
}
class Demo extends X, Y
{
   public static void main(String args[])
   {
      Demo obj = new Demo();
      obj.print();
   }
}

The reason is, in java you cannot extend two classes. In the above example class X and class Y has method print() with same name.

Also read – inheritance in java

class Demo is inheriting both class X and Y. Here we have a ambiguity to call which method.

So to solve this ambiguity, java does not allow multiple inheritance. But java supports multiple inheritance with the help of interfaces.


Multiple inheritance using interface

A class implementing more than one interface or interface extending more than one interface is multiple inheritance.

interface X
{ 
   void display(); 
}
interface Y
{ 
   void print(); 
}
class TestInterface implements X, Y
{ 
   public void display()
   {
      System.out.println("Hello World!!");
   }

   public void print()
   {
      System.out.println("Interface example");
   }
   public static void main(String[] args) 
   {
      TestInterface test = new TestInterface(); 
      test.display(); 
      test.print();
   }
}

Output:

Hello World!!
Interface example


Interface in JDK 8

Before JDK 8, interface had methods with no definition. But in JDK 8 we can have default implementation for interface methods.

interface Vehicle
{
   // default method
   default void display()
   {
      System.out.println("In Default method");
   }
}

class BMW implements Vehicle
{
   public static void main(String[] args) 
   {
      BMW obj = new BMW();
      obj.display();
   }
}

Output:

In Default method


JDK 8 Static method

In JDK 8 we have new feature static methods which are similar to static methods in classes which can be called without creating object.

NOTE: Static methods in JDK 8 are not inherited.

Let’s see a java program to demonstrate static methods in java 8,

interface Shapes
{
   static void print()
   {
      System.out.println("static method");
   }
}

class Square implements Shapes
{
   public static void main(String[] args) 
   {
      Shapes.print();
   }
}

Output:

static method


Use of interface

  • to achieve loose coupling
  • to achieve abstraction
  • supports multiple inheritance

Summary

In java programming,

  • An interface stipulates what class must do and not how to.
  • Interface is a blueprint of class.
  • Class overrides all abstract methods declared in interface.
  • In Interface methods are public and abstract by default.
  • Interface can contain only method signatures, constants, default methods, nested types and static methods.
  • Interfaces cannot be instantiated.
  • Interfaces can only be implemented by class or extended by other interface.
  • All methods and fields in interface are public, static and final by default and declared with empty body.
  • To declare an interface, “interface” keyword is used.
  • A class can’t implement two interfaces that have methods with same name.
  • In Java 8 version, interface can have static and default methods.
  • Meanwhile in Java 9, interface can have private methods.