Interface in java

Let’s learn interface in java.

Java interface

interface in java

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.


Syntax

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



Java interface example

To implement interface ‘implements’ keyword is used.

In interface Vehicle we have abstract method accelerate() and implementation is provided by class Audi and BMW 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 interface Vehicle 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.

NOTE:

  1. A class can extend another class. Similarly an interface can extend another interface.
  2. A class can extend many interfaces. But can extend only one 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 compiler 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.


Class vs Interface

  • Class does not support multiple inheritance whereas interface support multiple inheritance.
  • Classes have data members whereas interfaces do not have data members.
  • Class do have constructors whereas interface do not have constructors.
  • Class have incomplete and complete members whereas interface have incomplete members.
  • Class can have public, private, default and protected access modifiers whereas interface can have “public” as default access modifier.
  • Class can have static members whereas interface cannot have members as static.

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


Extending interfaces in java example

Here ‘extend’ keyword is used to extend interface. Let’s see an example,

public interface Car
{
   public void startEngine(String start);
   public void accelerateEngine(String accelerate);
}

public interface Audi extends Car
{
   // implementing class must implement these three methods
including two methods from interface Car
   public void typeOfEngine(String engine);
   public void carModel(String variant);
   public void passengerAirbag(int number);
}

public interface BMW extends Car
{
   // implementing class must implement these four methods 
including two methods from interface Car
   public void turboCharger();
   public void suspension();
   public void automaticClimateControl(int zone);
   public void tyreSize(int size);
}



Nested interface java

In java, like classes interface can be nested. Let’s see an example,

// outer interface
public interface Demo 
{
   void print();
   // inner interface
   interface inner
   {
      void displayPrimeNumber();
   }
}


class InterfaceDemo implements Demo.inner
{
   public void displayPrimeNumber()
   {
      int a = 0;
      int number = 0;
      String strPrimeNumber = "";
      for(a = 1; a <= 100; a++)
      {
         int counter = 0;
         for(number = a; number >= 1; number--)
         {
            if(a % number == 0)
            {
               counter = counter + 1;
            }
         }
         if(counter == 2)
         {
            strPrimeNumber = strPrimeNumber + a + " ";
         }
      }
      System.out.println("Prime numbers from 1 to 100 : ");
      System.out.println(strPrimeNumber);
   }

   public static void main(String[] args) 
   {
      Demo.inner obj = new InterfaceDemo();
      obj.displayPrimeNumber();
   }
}



Output:

Prime numbers from 1 to 100 :
2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97


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


Advantages of interface

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

Summary

In java programming,

  • Interface provide full abstraction.
  • Class overrides all abstract methods declared in interface.
  • All 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. We cannot create object of an interface.
  • Interfaces can only be implemented by class or extended by other interface.
  • Variables declared in interface are static, public and final by default.
  • 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.