Java - Interfaces

What is Interface?
An interface is a blueprint of class. It has constants and abstract methods. We will see in the past abstraction tutorial interface give 100% abstraction, so it is called fully abstraction.

The interface definition states the names of the methods and their return types and argument signatures. There is no executable body for any method that is left to each class that implements the interface.

Interface also represent IS-A relationship.


Why use interface?
-    It is use to achieve fully abstraction.
-    By using interface we achieve multiple inheritances.


Syntax:
The interface keyword is used to declare an interface.
 


public interface Interface-name{
	//Any number of final, static fields
  	//Any number of abstract method declarations
}



Example:


public class Main {
        
    public static void main(String[] args) {
         
          shape circleshape=new circle();
           
             circleshape.Draw();
    }
}
 
interface shape
 {
     public   String baseclass="shape";
      
     public void Draw();     
      
 }
 class circle implements shape
 {
 
    public void Draw() {
        System.out.println("Drawing Circle here");
    }
      
      
 }


Use implements keyword to implement interface into class.



How to implement multiple inheritance using interface?
We can see in previous tutorial we cannot multiple inheritance using class but we can achieve this functionality using interface.


In that case the class must implement all the methods declared in all the interfaces implemented.

We can implement multiple interface by comma separated list after implements keyword.

Example:


 interface car
      {
        int  speed=90;
        public void distance();
      }
      interface bus
     {
       int distance=100;
       public void speed();
     }
      class vehicle  implements car,bus
    {
      public void distance()
     {
      int  distance=speed*100; 
      System.out.println("distance travelled is"+distance);
     }
       public void speed()
     {
       int speed=distance/100;
     }
    }
       class maindemo
     {
        public static void main(String args[])
      {
        System.out.println("Vehicle");
       Vechicle v1=new Vehicle();
       v1.distance();
       v1.speed();
      }
      }


Extending interface?
An interface can extends another interface like the way to the class extends another class using extends keyword. The child interface inheritance the method and member variable of parent interface.

Example:


interface Vehicle{
	public void displayColor();
}

interface Car extends Vehicle{
	public void showMiledge();
}



What is marker interface in java and why required?
Marker interface in java is interface with no member variable or methods or in simple word empty interface called marker interface in Java, also known as tagging interface. E.g.  Serializable, Clonnable.

Marker interface used to indicate something to compiler or JVM. So if JVM sees a Class is Serializable it done some special operation on it, similar way if JVM sees one Class is implement Clonnable it performs some operation to support cloning.


Example:


Interface MarkerInterface{

}


Summary
-    Interface functions should be public and abstract.
-    Interface fields should be public and final.
-    Use the Keyword interface to define an interface.
-    If you define a public interface with name myInterface the java file should be named as myInterface.java (Similar to public class definition rules).
-    A class implementing an interface should use the keyword implements.
-    No objects can be created from an interface.
-    Interfaces don't have constructors as they can't be initiated
-    An Interface can extends one or more interfaces.
-    You can define a reference of type interface but you should assign to it an object instance of class type which implements that interface.


Note: Java compiler adds public and abstract keywords before the interface method and public , static and final keyword before data members.

Note : A class implements interface but one interface extends another interface.