Interface in java

Let’s learn interface in java.

Interface in java

Interface is collection of methods of abstract type (having empty body). It’s similar to Abstract class.

interface in java

Interface is blueprint of class. Interface specify what class must do and not how to.

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 InterfaceName
{
   fields // by default interface fields are public, static final
   methods // by default interface methods are abstract, public
}


Implementing interface in java

To implement interface ‘implements’ keyword is used. In the below example interface Vehicle has abstract method accelerate() and we can see implementation of accelerate() method in class BMW.

Example

import java.util.*;
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)
   {
      BMW obj = new BMW();
      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:

  • A class can extend many interfaces. But can extend only one class.

Also read – Why multiple inheritance is not supported in java?


Difference between class and interface

Here’s the difference between class and interface in java.

  • “class” keyword is used to create class. Whereas “interface” keyword is used to create an interface.
  • class does not support multiple inheritance whereas interface support multiple inheritance.
  • class is instantiated to create objects while an interface can’t be instantiated.
  • class do have constructors whereas interface do not have constructors.
  • class have incomplete and complete method whereas interface have only (incomplete) abstract methods.
  • class access modifiers can be private, public and protected whereas interface access modifier is “public” by default.
  • class can be inherited by another class using “extends” keyword. While interface can be inherited by class using “implements” keyword and interface can be inherited by an interface using “extends” keyword.

Also read – multiple inheritance using interface in java


Difference between abstract class and interface in java

abstract classinterface
have instance methods that implements default beahviour.methods are implicitly abstract. No implementation.
may have non final variables.variables declared are by default public, static and final
members are protected, private etc.members are public by default.
abstract class can be extended using keyword “extends”.interface can be implemented using keyword “implements”.
abstract class can extend another java class and implement multiple interfaces.interface can only extend another interface.
cannot be instantiated. can be subclassed.absolutely abstract; cannot be instantiated

use of interface in java

  • used to achieve loose coupling.
  • used to achieve total abstraction.
  • used to achieve multiple inheritance.

Multiple interfaces in java

In java to implement multiple interfaces, seperate each interface with a comma.

Let’s see an example on multiple interface in java.

interface Hello
{
   public void display();
}
interface World
{
   public void print();
}
class SampleClass implements Hello, World
{
   public void display()
   {
      System.out.println("sample text");
   }
   public void print()
   {
      System.out.println("sample text");
   }
}
class MultipleInterfaceExample
{
   public static void main(String[] args)
   {
      SampleClass obj = new SampleClass();
      obj.display();
      obj.print();
   }
}


Output:

sample text
sample text


Summary

  • Interface methods do not have body.
  • Interface methods are by default public and abstract.
  • We cannot create object of an interface.
  • Interfaces can only be implemented by class. This class must override all its methods.
  • Attributes of an interface are by default public, static and final.
  • An interface cannot have constructor.
  • A class can implement more than one interface.