Learn interface in java

Welcome to flower brackets blog. Today we are going to learn interface in java.

learn interface in java

Interface is a collection of abstract methods. These abstract methods have only declaration without definition and when interface implements each and every method should be defined in the class.

Also read – learn abstraction in java

The interface is used to implement multiple inheritance in java.

In java programming,

  • An interface stipulates what class must do and not how to.
  • Interface is a blueprint of class.
  • Interface represents IS-A relationship.
  • 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.
  • Any class which implements interface should implement all methods declared in interface.
  • In Java 8 version, interface can have static and default methods.
  • Meanwhile in Java 9, interface can have private methods.

Declaring an interface

Syntax

Interface <interfaceName> 
{
   // 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
{ 
   void accelerate(); 
}

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

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

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

Output:

Audi car accelerating…

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

Now after learning above concepts relationship between a class and interface is a class implements an interface, an interface extends interface and a class extends class.


Java multiple inheritance by 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


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 method definition is searched from object created in main method, that is, class Demo object “obj”.

When “obj” calls print() method compiler can’t determine which print() method to execute.

Because class X and class Y has methods with same signature.

Also read – learn inheritance in java

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


An interface extends another interface. Here’s a java program,

interface A
{
   void display();
}

interface B extends A
{
   void print();
}

class InterfaceExample implements B
{
   public void display()
   {
      System.out.println("Hello World");
   }

   public void print()
   {
      System.out.println("Java");
   }

   public static void main(String[] args) 
   {
      InterfaceExample obj = new InterfaceExample();
      obj.display();
      obj.print();
   }
}

Output:

Hello World
Java


Java 8 Default method

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

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

class BMW implements Vehicle
{ 
   // implementation of accelerate abstract method
   public void accelerate()
   {
      System.out.println("BMW accelerating...");
   } 
}

public class InterfaceDemo 
{
   public static void main(String[] args) 
   {
      Vehicle obj = new BMW();
      obj.accelerate();
      // here default method is executed 
      obj.display();
   }
}

Output:

BMW accelerating…
In Default method


Java 8 Static method

In Java 8 we have new feature that is defining static methods in interface which are similar to static methods in classes which can be called without creating object.

NOTE: Static methods in java 8 are not inherited.

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

interface Shapes
{
   static int area(int a)
   {
      return a * a;
   }
   void print(); 
}

class Square implements Shapes
{
   public void print()
   {
      System.out.println("Area of square : ");
   } 
}

public class InterfaceStaticDemo 
{
   public static void main(String[] args) 
   {
      Shapes sh = new Square();
      sh.print();
      System.out.println(Shapes.area(8));
   }
}

Output:

Area of square :
64


Use of interface

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

Related Posts