Interface in Java


In Java programming language interface is referred to as the blueprint of a certain class. The interface in Java programming language has static constants and abstract methods only. We use interface in Java programming language to achieve abstraction.

In Java interface we can only add the abstract methods. The body of the methods should not be in the interface of java. In java programming language we use interface to achieve multiple inheritance and abstraction.

The “IS-A” relationship is also represented by the java interface but instances cannot be used in java interface like we did in an abstract class.


Why use Java interface?

In Java programming language we use interface for the following reasons:

  1. To achieve abstraction
  2. To support the functionality of multiple inheritance
  3. To achieve loose coupling

When the interface method is used the abstract and public keywords are automatically added by the compiler. The keywords public, static, and final are also added before the data members by the compiler.

We can say that the fields of java interface are public, static, and final. And the methods are public and abstract.

Suppose we have written the code as:

CODE:

interface Example {

int id = 122;

void display ();

}

Then after the compilation of this code our program will look like the following:

interface Example {

public static id = 122;

public abstract void display ();

}

It can be seen that public, static keywords are added to the data member and public, abstract keywords are added to the method.


Understanding relationship between classes and interfaces:

In Java programming language classes extend other class and interfaces extend other interfaces but the implementation of the interfaces is provided by the classes.


Simple example of Java interface:

Consider the following example in which we have an interface and it has one method. The implementation of the interface is provided by a class:

CODE:

interface Example {

void display ();

}

class B implements Example {

public void display () {

system. out. prinln (“Hello Java”);

}

public static void main (string args []) {

B b1 = new B ();

b1. display ();

}

OUTPUT:

Hello Java

In the above example the interface has only one method then the class B is used to implement the interface in which there is the body of the method of the interface.


Multiple inheritance in Java by interface:

In java programming language if some class implements a number of interfaces then this will be called multiple inheritance. In other words we can say that multiple inheritance is when an interface extends multiple interfaces.

Consider the following diagram to understand what multiple inheritance by interface in java is:

multiple-inheritance-in-java-by-interface

Consider the following example in which there are multiple interfaces and are implemented by class:

CODE:

interface Example {

void display ();

}

interface Example1 {

void print ();

}

class B implements Example, Example1 {

public void display () {

system. out. println (“Hello World”);

}

public void print () {

system. out. println (“Hello Java”);

}

public static void main (string args []) {

B b1 = new B ();

b1. display ();

b1. print ();

}

}

OUTPUT:

Hello World

Hello Java

In the above example two interfaces namely Example and Exampl1 has one method each. The implementation of these interfaces is provided by the class B and the body of the methods of these interfaces is also in the class. Then in the main () method object of the class is created and is used to invoke the methods of interfaces.


Q. Multiple inheritance is not supported through class in java but it is possible for interface why?

In Java programming language multiple inheritance is not supported by classes. But through interface multiple inheritance is possible because the implementation of these interfaces are provided by class.

Consider the following example:

CODE:

interface Example {

void display ();

}

interface Example1 {

void display ();

}

class B implements Example, Example1 {

public void display () {

system. out. println (“Hello World”);

}

public static void main (string args []) {

B b1 = new B ();

b1. display ();

}

}

OUTPUT:

Hello World

In the above example both interfaces has same method. As the implementation is provided by class there is no ambiguity and multiple inheritance is achieved.


Interface inheritance:

In java interface extends another interface but its implementation is done by class.

Consider the following example:

CODE:

interface Example {

void display ();

}

interface Example1 extends Example {

void print ();

}

class B implements Example1 {

public void display () {

system. out. println (“Hello World”);

}

public void print () {

system. out. println (“Hello Java”);

}

public static void main (string args []) {

B b1 = new B ();

b1. display ();

b1. print ();

}

}

OUTPUT:

Hello World

Hello Java


Q. What is marker or tagged interface?

In Java, marker or tagged interface is that which has no member.


Nested interface in Java:

A nested interface is an interface that has another interface in it. Consider the following example:

interface Example {

void display ();

interface Example1 {

void print ();

}

}