What is an interface and how to work with an interface in java

An interface is a group of final variables and unimplemented  methods.


interface in java Interface contains all unimplemented methods,having no body .
interface in java The variables of an interface are public and final (ie constants) .
interface in java Interface can extends another interface but not possible to implement .
interface in java We cannot instantiate an interface .
interface in java Interface reduced the multiple inheritance problem in java .
interface in java All methods of an interface is by default public and abstract .
interface in java Each and every method in an interface terminates with a semicolon (;) .
interface in java Methods of an interface should not allow static , final and synchronized access
……modifiers .
interface in java Methods of an interface should not allow private and protected access specifiers .



public (or) abstract interface <interface_name>
//final variables
//public (or) abstract <return type> <method name>(perameter1,perameter2, ….);

interface in java Work with an interface :

interface in java  implements more than one interfaces :
In java multiple inheritance concept does not support like c++.Just imagine one class derived from two base classes.If the two base classes having exactly the same method or methods , in this case which method is to be used by the derived class? For this problem we can get a partial solution like implementing more than one interfaces.
Here is an another Question, if the two implemented interfaces having exactly the same method or methods ,which method will be used by the implemented class? In this case, the implemented class provides single implementation for the two methods which is exactly the same in the two interfaces.See the example code below.

Execution Result:

Hence the two interfaces is having exactly, the same method.But we implemented only once.So it will be the common implementation for the two identical methods from the both interfaces.
interface in java Sub interface having one or more base interfaces (extends):
We can derive an interface from one or more super interfaces by using extends keyword like below.


interface in java  A class that implements an interface:
In this case , the implemented class should implement all the methods from the interface.If not it will throw an error.At  least implement the methods with an empty body.

Execution Result:


interface in java  Implementing an interface inside a class:
Here we are implementing an interface inside a class without  using  implements  key word.

Comparator interface is from java.util package.This interface having two methods compare() and equals().
We are implementing compare() method to sort the given data by its name.


interface in java  Implementing an interface inside a method:
Below code represents , how to implement an interface inside a method.


interface in java  class in an interface:

interface in java  It is possible to write a nested or inner class in an interface.

Related Posts :
What is the difference between interface and abstract class in java
What is Map interface in java
What is List interface in java
What is Set interface in java
   [will not be published]

^ <