Introduction

  1. From the client point of view interface defines the set of services what he is getting.
  2. From the service provider point of view an interface defines the set of services what he is offering.Hence an interface acts as a contract between client and service provider.
The Main Advantages of interface are
  1. Security :As we are not highlighting our internal implementation details,An outside person is not allowed to get internal logic,hence we achieve security.
  2. Enhancement: With out effecting end user or client we can perform any modification in the internal implementation.
  3. Improves Maintainability.
  4. Interface never allowed to contain any implementation details.Hence all the methods declared inside interfaces must be abstract and interface is considered as 100% pure abstract class.

Interface Declaration

We can declare an interface by using interface keyword.

interface Sample{
  public void m1();
 }
class Test implements  Sample{
  public void m1(){
     //implementation
  }
}
  1. When ever we are providing implementation for an interface method,then we should declare that method as public,otherwise we will get compile time error.
    interface Sample{
     void m1();
    }
    class Test implements Sample{
     void m1(){}
    }
    /*
    Compile time error:
    m1() in Test cannot implement m1() in Sample; attempting to assign weaker access privileges; was public
    */
    
  2. When ever we are providing implementation for an interface,then we should provide implementation for all the interface methods,otherwise we have to declare that class as abstract.
    interface Sample{
     void m1();
    }
    class Test implements Sample{
     
    }
    /*
    Compile time error:
    Test is not abstract and does not override abstract method m1() in Sample
    */
    

    If Test class declared as abstract then we won’t get any compilation error.
    Note:If the Test class declared as abstract,then the child class of Test is responsible to provide implementation for m1() method.

  3. An interface contains only abstract methods and there is no chance of having concrete methods.Hence an interface can be considered as 100% pure abstract class.
  4. The applicable modifiers for the interface are
    • public
    • abstraction
    • strictfp
    • default

Note: We are not allowed to declare an interface as final.

interface Methods

Every Interface method is by default public and abstract whether we are declaring or not.Hence the following declarations are equal inside interface.

void m1();
public void m1();
public abstract void m1();

As the interface method is already public and abstract, we are not allowed to declare with the following modifiers.

  • private
  • protected
  • static
  • final
  • native
  • strictfp
  • synchronized

Interface variables

An interface can contain variables also and every interface variable is by default public static and final.Hence the following declarations are equal inside interface.

int i = 10;
public int i = 10;
public static int i = 10;
public static final int i = 10;

As interface variables already public static and final,hence we are not allowed to declare with the following modifiers.

  • private
  • protected
  • volatile
  • transient

For the interface variables,we should perform initialization at the time of declarations only otherwise we will get compile time error.

Example:

interface Sample{
 int i ; 
}
/*
Compile time error:
Sample.java:2: = expected
 int i;
      ^
*/

interface variables are by default available in the implemented classes.From the implementation classes we are allowed to access interface variables but we are not allowed to change their values i.e reassignment is not possible because these are final.

Example:

interface Sample{
	int i=10;
}
class Test implements Sample{
	public static void main(String arg[]){
		i = 20;
		System.out.println(i);
	}
}
/*
 cannot assign a value to final variable i
  i = 20;
  ^
*/

If place int i = 20 instead of i = 20; then we won’t get compiler error because it is local variable to main method.

extends vs implements

  1. A class can extends only one class at a time.
  2. A class can implement any number of interfaces simultaneously.
  3. An interface can extend any number of interfaces simultaneously.

Naming conflicts in interfaces

Case1:
If two interfaces contain a method with same signature and same return type then in the implementation class,we have to provide implementation for one method.

Example:

interface Left{
 public  void m1();
}
interface Right{
 public  void m1();
}
class Test implements Left, Right{
 public void m1(){
 System.out.println("java2learn");
 }
}

Case2:
If two interfaces contains a method with the same name but different arguments. In the implementation class,we should provide implementation for both methods and these methods act as overloaded methods.

Example:

interface Left{
 public void m1();
}
interface Right{
 public void m1(int i);
}
class Test implements Left, Right{
 public void m1(){
  System.out.println("m1 method no args");
 }
 public void m1(int i){
  System.out.println("m1 method with args");
 }
}

Case3:
If two interfaces contains a method with same signature but different return type then we can’t implement those two interfaces simultaneously.

interface Left{
  public int m1();
}
interface Right{
 public void m1();
}
class Test implements Left, Right{
 
}

Variable naming conflicts

Example:

interface Left{
 int i = 10;
}
interface Right{
int i = 100;
}
class Test implements Left, Right{
public static void main(String ar[]){
  System.out.println(i);// C.E reference to i is ambiguous
  System.out.println(Left.i);//10
  System.out.println(Right.i);//100
 }
}

Marker Interface

  1. By implementing an interface,if we are getting some ability,such type of interfaces are called “marker” interface or “tag” interfaces.
    Example:Serializable, Clonable,Comparable
  2. If an interface doesn’t contain any method it is always marker interface.
    Example:Serializable, Clonable,RandomAccess
  3. Even though an interface contains some methods,By implementing that interface,if we are gettingsome ability still we can consider that interface are called ‘marker’ or ‘tag’ interface.
    Example:Comparable, Runnable.